jQuery is every Asp.Net web applications best friend.  I am going to show you the easies way to interact with an Asp.Net page using JSON.  JSON (JavaScript Object Notation) allows you to pass objects between .Net and Javascript.   If you are using .Net 3.5 then you have JSON serialization of your objects on the .Net side covered.  If you are using a version of .Net prior to that I recommend you look at Json.net.

Calling the server from the clients page using jQuery with a great plug-in called jmsajax (jQuery Microsoft Ajax)

$.jmsajax({
   type: "POST",
   url: "Default.aspx",
   method: "UpdateSomething",
   dataType: "msjson",
   data: {Id:"text", name: "someName"},
   success: function(result) {
    if(result.Error)
    {
        page.someDiv.html(result.Message);
    }
    else
    {
        page.otherDiv.html("Something Successfully Saved");
    }
   }
});

Jmsajax fixes some issues with using jQuery with Asp.net.  The most important issue to note is that it corrects an issue with passing dates back and forth between Asp.net and JavaScript

The Server Side Web Method used to create and return our POCO as JSON

 

 [WebMethod(true)]
public static Result UpdateSomething(string Id, string name)
{
    Result result = new Result();
    result.Error = false;
    result.Message = "The Cost Per Unit must be filled out";
    return result;
}

 

And there you go now you can call a method on the page behind from JavaScript.  Now go and add some Ajax functionality to your web sites.

Submit this story to DotNetKicksShout it   Bookmark and Share