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"
EnablePageMethods="True">
</asp:ScriptManager>
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
[System.Web.Services.WebMethod]
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"
OnClientClick="return fun()"/>
In javascript function we can use PageMethods object to call
the WebMethod of the page.
function fun()
{
PageMethods.GetStatus(onSucceed, onError);
return false;
}
function onSucceed(result)
{
alert(result);
}
function onError(result)
{
}
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:
PageMethods.GetStatus(“p1”,”p1”,onSucceed, onError);
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:
[System.Web.Services.WebMethod]
public static string GetStatus(string s1, string s2)
{
return (string)HttpContext.Current.Session["key"];
}
We can also return the application and cache data in the
similar way.
return (string)HttpContext.Current.Application["key1"];
return (string)HttpContext.Current.Cache["key"];
Script Manager and Page Methods
The ScriptManager renders the java script to access the web
methods when the EnabledPageMethods is set to “True”. An JavaScript object
Page Methods is created using which we can access the web methods of the
page.
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:
PageMethods.Somemethod(p1,p1,p3,onSucceed,onError);
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
usage.