0
votes

This seems to be a common question but I could not find a relevant answer

I have a aspx page and in it, I've made a list of "a href" tags that is generated from my C# code behind. Now i want to send data based on which of the "a href" tag i have selected and pass it into my C# code which will then populate my popup with required information:

The process

"a href click" -> call c# method -> populate popup with data

I am stuck at how to pass data from the "a href click" to the c#.

I tried using .OnServerClick but my popup didn't even pop up.

For starters, i would like to try: When i Click a "a href" tag it would call my changeTitle() method from c# which will change the title of the pop up. If i can get this working, i should be able to manage the rest

popup

c# method:

public void changeTitle()
{
    this.modalTitle.InnerHtml = "Goodbye world";
}

Do tell me if you need more information please, I really hope to get this working

2
Did you hear about query strings?TGlatzer
no, what is it? Jquery?Bloopie Bloops
A query string is part of the url: example.com/?myParamName=myParam The query string is behind the ? Your server can receive this dataTGlatzer
But a little less firm: You should consider using data-xxx attributes, which are nicely available in jQuery and make an AJAX call to your server, which then delivers the data or the html snippet for the overlay windowTGlatzer
but I have to send the data to my C# side to call a method before it gets back to my .aspx pageBloopie Bloops

2 Answers

3
votes

You should really look into learning more about how jQuery and ASP.NET can work together. I highly recommend starting at Dave Ward's site.

Here is a blog entry about using jQuery to call ASP.NET server methods (page methods are a great way to get quick hooks into server-side logic that can pass data back to the client-side): Using jQuery To Directly Call ASP.NET AJAX Page Methods

I have used this technique in many projects over the years and I think once you start learning the power of jQuery you will want to use this approach over strictly server-side controls.

1
votes

If ASP.NET WebForms is being used (remember to specify which "flavor" of ASP.NET), a LinkButton control may be a suitable approach1.

A LinkButton works like a a normal Button that looks like a hyperlink and causes a PostBack when clicked. (This is different from "normal" hyperlinks that change the browser location.)

The OnClick attrtibute specifies the server callback handler and the Command and CommandArguments can be used to associate specific data with the control that are available on the server during the LinkButton's click callback.


1 While my current preferred form of development is a thick client with a thin backend (e.g. not WebForms), switching to use "Page Methods" or "AJAX" requires rewriting the front-end/HTML to act on the response as appropriate. A LinkButton on the other hand, simply "works" with the normal ASP.NET WebForms lifecycle/infrastructure without any additional work.