I need to fire asp.net (link button or Button) click event(server side code) using Jquery, the buttons are in an update panel. Kindly help.
-
1why don't you create web method with your server side code and call it from jquery post?Jayantha Lal Sirisena– Jayantha Lal Sirisena2011-05-31 11:54:58 +00:00Commented May 31, 2011 at 11:54
-
As Jayantha said you need to create a webmethod. I have explained belowAli Habibzadeh– Ali Habibzadeh2011-05-31 11:57:40 +00:00Commented May 31, 2011 at 11:57
-
I cannot JQuery ajax and i have tried the JQuery trigger method but it doesn't seem to work in my application. I need alternaltive to document.getElementById('lnkMyButton').click(); which will support multiple browsersskamale– skamale2011-05-31 12:18:56 +00:00Commented May 31, 2011 at 12:18
-
If you have code you tried and isn't working, please post.mlibby– mlibby2011-05-31 12:28:12 +00:00Commented May 31, 2011 at 12:28
4 Answers
Michael's solution is good. But I think it is safer to call GetPostBackEventReference. The internal structure of ASP.NET Page may be changed in the future.
Here's the sample Code.
<%= Page.ClientScript.GetPostBackEventReference(lnkButton, "") %>
Comments
How about:
__doPostBack("<%= lnkMyButton.UniqueID %>", "");
2 Comments
What you need to do is to define your server side code as [WebMethod] once you do that your classname will be available to client side code for calling.
Then you would go about calling that method using something like this:
jQuery.ajax({
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: '{}',
dataType: 'json',
url: 'MyPage.aspx/SomePageMethod',
success: function(result){
alert(result);
}
});
I am more of a C# person, but I would imagine if you read this page you are easily able to make it in VB: http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/
Comments
Below is how I triggered a click event with a LinkButton within an Update Panel. I couldn't get the ID until run time due to the way I was creating buttons, that is, it kept appending a _0, _1, etc to the end of the button name. For example on the server I would create 2 buttons with ID's say of 111555 & 222666. It would rename the buttons with ID's like:
contentMain_gridviewMessages_111555_0
contentMain_gridviewMessages_222666_1
So the first thing I did was create a unique attribute I could search upon, in my case the message id:
var msgLink = ('a[messageId="' + messageId + '"]');
After that I used a little bit of jQuery to get the auto generated ID:
var id = $(msgLink).attr("ID");
When I got the ID good ole fashion javascript did the rest:
document.getElementById(id).click();
Hope this helps someone out.