How to invoke ASP.NET Page Methods using jQuery
Why do I want to invoke ASP.NET Page Methods using jQuery?
- I do not want the ScriptManager to generate any proxy code in my page source, i.e. set EnablePageMethods to “false”.
- I want to make the call synchronous! (N.B. You should avoid making synchronous calls!)
Show me the code:
$.ajax({
async: true, /* set this to false to make a synchronous call */
contentType: "application/json; charset=utf-8",
data: Sys.Serialization.JavaScriptSerializer.serialize({ arg1, arg2, etc... }),
dataType: "json",
type: "post",
url: "application_path/page.aspx/page_method_name",
success: function(result) {
// Insert your success handler code here.
}
});
I am assuming the Microsoft AJAX Library is included on the page, and I am using Sys.Serialization.JavaScriptSerializer.serialize to prepare the page method call arguments into JSON.
You never know when synchronous call may be required. My present love of effort requires me to enhance lookup so that it dynamically populate sql parameters with form fields’ values. I need to use synchronous so that I can update the sql in session variable, and thereupon invoke call to the lookup dialog.
Thanks for this barebone wonder.
Ranjit Kumar
March 10, 2010 at 12:38 am