To enable Page methods we need to drag a ScriptManager
control to the page and mark
the EnablePageMethods to “True”.
<asp:ScriptManager ID="ScriptManager1" runat="server"
Go the Code behind file of the page and add a static method
and mark them as WebMethod. Lets Say the method name is GetPageMethod which
returns a status value
public static string GetPageMethod()
return "Welcome PageMethods";
Now that the method is exposed add a button control and call
a client script which accesses the Webmethods of the page
<asp:Button ID="Button1" runat="server" Text="Call Page Methods"
the WebMethod of the page.
Now run the application and check the result which shows the
alert box with value “Active”. The sample application calls the webmethods of
the page using client script without full page cycle.
Let’s say if the page method has two input parameters we can
pass paramters as given in the below sample:
We can also access the Session , Application and Cache
variables data using the Page Methods. For example:
protected void Page_Load(object sender, EventArgs e)
HttpContext.Current.Session["key"] = "SomeValue";
HttpContext.Current.Application["key1"] = "SomeValue1";
Accessing the session data in the page methods:
public static string GetStatus(string s1, string s2)
We can also return the application and cache data in the
Script Manager and Page Methods
The ScriptManager renders the java script to access the web
Page Methods is created using which we can access the web methods of the
In order to call web methods using PageMethods object we
should add a function for Successful Page method call and a function for
UnSuccessful Page method call as seen in our sample above. These functions can
be treated as Callback functions.
We can also pass parameters to the page method along with
references to call backs separated by “,”.
We can pass n number of parameters to the page methods from
the client script which are required for processing and getting the result.
The syntax for calling the page methods using PageMethods
object and passing parameters:
Internally the script manager builds the PageUrl and
XmlHttpRequest and then post the request to the server to fetch the required
result. Ultimately its all the XmlHttpRequest stuff that is wrapped up for easy