As PageMethods in ASP.NET need to be static methods and marked as web method, I was in a impression that instance of that particular page won’t be created when a page method is called.
But when I tried putting a break point in the constructor it was getting hit every time a pagemethod is called. Can somebody let me know what is need for constructing an instance?
Thanks
Server side
public partial class PageMethod : System.Web.UI.Page
{
public PageMethod()
{
}
[System.Web.Services.WebMethod()]
public static string GetMessage()
{
return "Page Method Call";
}
}
Client Side (used JQuery)
$.ajax({ type: 'POST',
url: /PageMethod.aspx/GetMessage,
data: null,
success: onSuccess,
contentType: "application/json; charset=utf-8",
dataType: 'JSON',
error: onError
});
EDIT
Indeed constructor is called!! See callstack of pagemethod’s call:
ASP.NET engine routes request from pagemethod to aspx-handler-factory (PageHandlerFactory). As far as I understand it’s inner logic of ASP.NET. Therefore it’s correct 🙂
Now check asmx-webmethod call:
Here using another httphandler-factory – ScriptHandlerFactory.
PS Moorthy, thanks! Good question 🙂 ‘Live and learn!’
static / [WebMethod] – cos page method is called solely by ajax (without page creation).
If you wanna have Page instance you should use UpdatePanel or AJAX.NET Controls (for instance DevExpress, AjaxToolKit).