3

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.

4
  • 1
    why don't you create web method with your server side code and call it from jquery post? Commented May 31, 2011 at 11:54
  • As Jayantha said you need to create a webmethod. I have explained below Commented 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 browsers Commented May 31, 2011 at 12:18
  • If you have code you tried and isn't working, please post. Commented May 31, 2011 at 12:28

4 Answers 4

4

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, "") %>
Sign up to request clarification or add additional context in comments.

Comments

2

How about:

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

2 Comments

hi @Michael, this works fine! but will you please explain about the second parameter that you set to blank?
@SHEKHARSHETE It's the event argument, the first parameter is the event target. Normally a button is the only thing that can trigger a post, so for LinkButtons (which are just A tags), TextBoxes with AutoPostBack=true and OnTextChanged event, etc ASP.NET uses this __doPostBack method it creates to set two hidden fields (the eventTarget so ASP.NET knows what triggered the post and eventArgument data the server control may need) and then calls document.forms["form1"].submit() to trigger the post. I've never seen LinkButton control use the eventArgument but other server controls may use it.
1

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

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.