Skip to content Skip to sidebar Skip to footer

Fire Asp.net (link Button Or Button) Click Event Using Jquery

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.

Solution 1:

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, "") %>

Solution 2:

How about:

__doPostBack("<%= lnkMyButton.UniqueID %>", "");

Solution 3:

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/

Solution 4:

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.

Post a Comment for "Fire Asp.net (link Button Or Button) Click Event Using Jquery"