griegs griegs - 1 year ago 186
C# Question

jQuery Postback with Webforms

We're redevloping a major section of our website and rather than use a 90k AJAX file I'd rather use a 19K jquery script.

I've seen the following articles;

The thing I don't get is how to do a postback to a specific method in either the code behind or another class.

I know in ASP.NET-MVC I can post back to a controller / action. How do I call a particular method in WebForms?

Something along the lines of; $.post("class and action", ( param:value}......

Any thoughts, code etc???

Answer Source

It is very easy to call specific methods in code-behind. Here is nice article with all the details by Dave.

Simply declare a method like this:

  public static string GetDate()
    return DateTime.Now.ToString();

This is all you need in jQuery:

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


  1. WebMethod must be on a static method
  2. Must stringify posted data if sending anything (i.e. JSON.stringify(yourDataObject)), will be deserialized according to method parameters
  3. msg is the response, the return result from your method is in the property msg.d
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download