One the cool features I have used is calling one of my .NET methods using the “$.ajax” jQuery command. In my example (below), I have created two aspx pages. The code-behind of my first page (jQueryMethodTest.aspx) will only contain a public static method called “WhatIsYourName”, which returns a string value.
Remember, the jQueryMethodTest.aspx page only needs to contain our method nothing else! Additional methods can be added. Just don’t add any web controls.
The second page (jQueryAjax.aspx), will contain our jQuery code and some HTML to output our result from calling the “WhatIsYourName” method.
If all goes well, you should get the following result:
The “$.ajax” jQuery command requires the following parameters in order to work:
- url – links to where our .NET method is placed.
- data – retrieves the value from some control in our page to pass to our method. Remember, the name of the parameter must be named the same as the parameter from our .NET method.
- dataType – the response type.
- contentType – the request content type.
I guess jQuery’s motto really is true: “write less, do more”.