Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hey guys, I'm struggling with finding a good guide for this and figured I might be looking up the wrong keywords.

A friend mentioned Ajax might be perfect for this but I haven't used it so could do with some advise on what to do next.

I basically want to click a html button in then using JavaScript send the ID of that button server side to a Select statement, the select statement would fill a Hidden field and if populated successfully the client side would use that hidden field to display a list of information

I have multiple buttons like this so it has to be possible to do over and over without refreshing the page. The select statement where clause depends on the button clicked.

The server side code is C#

Can I get some help or advise on this? If I wasn't clear enough please tell me and ill try to reword my questions, Thanks in advance.
Posted
Updated 19-Feb-13 1:10am
v2
Comments
Asim Mahmood 19-Feb-13 7:45am    
you must use jQuery for your goal.
Ankur\m/ 19-Feb-13 8:03am    
"must"????
WHY? The same thing can be done using javascript.

Here is a nice and short article article on one of the ways on how you can do this: Using jQuery to directly call ASP.NET AJAX page methods[^].

Try this out and let me know how it works for you!
 
Share this answer
 
Comments
Phoenix234 19-Feb-13 7:47am    
does the C# function HAVE to be static? as soon as I changed it to static this line became an error as the hidden field isnt allowed in a static method?

hfMenu.Value = ser.Serialize(Menus);
Ankur\m/ 19-Feb-13 7:58am    
Yes, the method must be static.
Don't assign the value to the hidden field in your method. Rather return the value and then assign to hidden value / use it at the client side.
Ankur\m/ 19-Feb-13 8:02am    
Just wanted to let you know that I updated the comment with a solution, in case you haven't seen the updated version.
Phoenix234 19-Feb-13 8:47am    
So far this has worked great, is there a way I can use the returned value to fill the c# HiddenField using the client Ajax?

then I can call the display function which is already expecting a HiddenField as input and run it like I did before without modifying much of my code.
Ankur\m/ 19-Feb-13 10:39am    
Sorry for the late reply... was out for dinner.
Yes you can. Do you see the success function in the article. It has a parameter called msg. That is what will contain your result, if you have passed the value correctly. Install Firebug (Firefox plugin) and debug the javascript code. Add watch for 'msg' and check what it contains. The article already has ways to use the value.
Finally when you have identified how to use your return parameter, assign the value to HiddenField using
JavaScript - document.getElementById("HiddenFieldId").value = msg....;
/jQuery - $("#HiddenFieldId").val(msg...);
Hope that helps!
Hi,

I suggest the following steps to you.

-> Add ASP.NET button instead of HTML button, since there is no post back facility in HTML button.
-> Add custom attribute "tag" to that button.
ASP.NET
<asp:Button ID ="ID1" runat="server" tag="test" ..

the value for tag is your wish, based on what you want to differentiate the buttons.

-> In server side event, handle the event as you are doing currently based on the button.
-> Even if many buttons, you handle in single event function only.
-> in the event function identify the button, based on the tag value.
C#
string tag = ((Button)sender).Attributes["tag"].ToString();

-> execute the select statement and display the details in the screen.

hope it helps.
 
Share this answer
 
v2
Comments
Phoenix234 19-Feb-13 7:45am    
Unfortunately the buttons are generated in Javascript and have to be HTML.

Basically I make a list of database items, each item has a "view menu" button which is created when that row is read from another hidden field.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900