The page method is only a public static method with the webmethod property exposed in the page code class. The page method is almost the same as the runtime engine of Web services that support Ajax. Using the PAGE method, we do not need to create or publish services.
PAGE method definition
Define a public static method in the page code class and add the webmethod feature to it to convert the ASP. NET Ajax page to a web service:
public class TimeServicePage : System.Web.UI.Page
{
[WebMethod]
public static DateTime GetTime()
{
return DateTime.Now;
}
}
We can use any data type in the PAGE method, and all types will be converted to JSON format during the call.
We can also define the web method in inline mode in the aspx source file:
<script type="text/C#" runat="server">
[WebMethod]
public static DateTime GetTime()
{
return DateTime.Now;
}
</script>
Note that the page method is for a given ASP. NET page. Only the host page can call its own web method. Cross-page calling is not supported.
Enable the PAGE Method
If the code class on the ASP. NET Ajax page contains the static method modified by webmethod, the runtime engine will generate the same JavaScript proxy class as the web service. We can use the implementation of this class to call the server method. The name of this class is hard-coded as pagemethods.
Note that the page method is disabled by default. To enable it, we must set the enablepagemethods attribute of the script manager to true.
<asp:ScriptManager runat="server" ID="ScriptManager1" EnablePageMethods="true" />
To execute the PAGE method, the HTTP module scriptmodule must be in the web. config file:
<add name="ScriptModule"
type="System.Web.Handlers.ScriptModule, System.Web.Extensions" />
The module listens to application events after the session status is loaded, executes the PAGE method, and generates a response for the caller. Because obtaining the session status is executed before the page lifecycle is started, this method does not create a page lifecycle or initialize and process child controls.
Use of page Methods
The set of page methods is exposed to the JavaScript code pagemethods with a fixed class name. This class lists all static methods and does not require self-instantiation.
Pagemethods proxy class
Unlike the Web Service proxy class, the pagemethods proxy class is always embedded in the page subject that references it in the form of an internal script. Otherwise, the names of different pages should be different. The structure of this class is almost the same as that of the Ajax Web Service proxy class.
Page Method execution
The gettime method in the above code is used as an example:
function getTime()
{
PageMethods.GetTime(methodCompleted);
}
function methodCompleted(results, context, methodName)
{
var displayString = results.format("ddd, dd MMMM yyyy");
$get("Label1").innerHTML = displayString;
}
Through the PAGE method, we can use the current attribute of httpcontext to access view status, ASP. NET cache, user, and other internal objects. HTTP context does not target the lifecycle of a page. It exists after a request is received and is always accompanied by a request.