Jquery asp.net Button Click Event via ajax Jquery asp.net Button Click Event via ajax asp.net asp.net

Jquery asp.net Button Click Event via ajax


This is where jQuery really shines for ASP.Net developers. Lets say you have this ASP button:

When that renders, you can look at the source of the page and the id on it won't be btnAwesome, but $ctr001_btnAwesome or something like that. This makes it a pain in the butt to find in javascript. Enter jQuery.

$(document).ready(function() {  $("input[id$='btnAwesome']").click(function() {    // Do client side button click stuff here.  });});

The id$= is doing a regex match for an id ENDING with btnAwesome.

Edit:

Did you want the ajax call being called from the button click event on the client side? What did you want to call? There are a lot of really good articles on using jQuery to make ajax calls to ASP.Net code behind methods.

The gist of it is you create a static method marked with the WebMethod attribute. You then can make a call to it using jQuery by using $.ajax.

$.ajax({  type: "POST",  url: "PageName.aspx/MethodName",  data: "{}",  contentType: "application/json; charset=utf-8",  dataType: "json",  success: function(msg) {    // Do something interesting here.  }});

I learned my WebMethod stuff from: http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/

A lot of really good ASP.Net/jQuery stuff there. Make sure you read up about why you have to use msg.d in the return on .Net 3.5 (maybe since 3.0) stuff.


I like Gromer's answer, but it leaves me with a question: What if I have multiple 'btnAwesome's in different controls?

To cater for that possibility, I would do the following:

$(document).ready(function() {  $('#<%=myButton.ClientID %>').click(function() {    // Do client side button click stuff here.  });});

It's not a regex match, but in my opinion, a regex match isn't what's needed here. If you're referencing a particular button, you want a precise text match such as this.

If, however, you want to do the same action for every btnAwesome, then go with Gromer's answer.


ASP.NET web forms page already have a JavaScript method for handling PostBacks called "__doPostBack".

function __doPostBack(eventTarget, eventArgument) {    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {        theForm.__EVENTTARGET.value = eventTarget;        theForm.__EVENTARGUMENT.value = eventArgument;        theForm.submit();    }}

Use the following in your code file to generate the JavaScript that performs the PostBack. Using this method will ensure that the proper ClientID for the control is used.

protected string GetLoginPostBack(){    return Page.ClientScript.GetPostBackEventReference(btnLogin, string.Empty);}

Then in the ASPX page add a javascript block.

<script language="javascript">function btnLogin_Click() {  <%= GetLoginPostBack() %>;}</script>

The final javascript will be rendered like this.

<script language="javascript">function btnLogin_Click() {  __doPostBack('btnLogin','');}</script>

Now you can use "btnLogin_Click()" from your javascript to submit the button click to the server.