PageMethods In ASP.NET AJAX
page 2 of 4
by Suresh Kumar Goudampally
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 103771/ 68

Sample Application Demonstrating Page Methods

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.


View Entire Article

User Comments

Title: zhe   
Name: li
Date: 2012-12-18 8:10:18 PM
Comment:
test how
Title: akakakak   
Name: pusing
Date: 2012-12-05 11:40:34 PM
Comment:
buseeeeeeeeeeeee
Title: wqeqw   
Name: dasdas
Date: 2012-12-05 2:59:03 AM
Comment:
sdasdasd
Title: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaassssssssssssssssssssssssssssssssssddddddddddddddddddddddddddddddddddddddddddxxxxxxxxxxxxxxxxxxxxxxx   
Name: wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwdddddddddddddddddddddddddddddddaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Date: 2012-11-28 4:18:36 AM
Comment:
asdddddddddddddddddddddddddcas testing#1
Title: thx   
Name: thx
Date: 2012-11-28 4:16:33 AM
Comment:
thx
Title: aaa   
Name: sss
Date: 2012-11-19 7:25:12 AM
Comment:
fff
Title: سيش   
Name: سشي
Date: 2012-10-23 8:31:23 AM
Comment:
سيش
Title: Mr.   
Name: Farooque
Date: 2012-10-19 10:05:08 AM
Comment:
sfsdf
Title: u   
Name: hgj
Date: 2012-10-17 7:36:06 AM
Comment:
ghjhg
Title: ddd   
Name: dd
Date: 2012-10-16 6:29:48 AM
Comment:
ddd
Title: hefrhfb   
Name: dfnejdfklndg
Date: 2012-09-04 2:59:42 AM
Comment:
fnfjrklmnfensxf
Title: zz   
Name: zz
Date: 2012-08-30 9:53:06 AM
Comment:
zzz
Title: tttyu   
Name: fdfdghf
Date: 2012-08-11 8:41:57 AM
Comment:
ghtjhk,l.k/l;l
Title: dgfdsg   
Name: dsgdsg
Date: 2012-07-26 2:00:31 AM
Comment:
dgsdgdsg
Title: rty   
Name: rty
Date: 2012-07-19 7:36:48 PM
Comment:
rty
Title: wwt   
Name: kevin
Date: 2012-06-09 12:21:15 AM
Comment:
dfaklj
Title: ds   
Name: ds
Date: 2012-06-06 3:15:53 PM
Comment:
sds
Title: bmbnnnnmbnm   
Name: nbmbnm
Date: 2012-05-18 6:07:18 AM
Comment:
nnnnnmbbbbbbbbbmmmmmmmmmmmmmmbbmbbbbbbbbbbbbbbmmmmmm
Title: Excellent   
Name: Srinivasa Rao D
Date: 2011-12-23 1:05:20 AM
Comment:
U save my day..!
Title: Invalid Method Name   
Name: Prakash
Date: 2011-07-08 4:53:41 AM
Comment:
Please change method name as GetPageMethod instead of GetStatus(). Those who are copying this code, that code wont run. check befor every one run. Thanks.
Title: Good One   
Name: sachin
Date: 2011-02-01 7:43:14 AM
Comment:
Understandable for those who are beginner for PageMethods....
Title: Nice Article   
Name: HPR
Date: 2011-01-24 8:41:55 PM
Comment:
Nice informative article.
Title: aa   
Name: aaa
Date: 2011-01-18 5:34:27 AM
Comment:
very nice
Title: why does not work in firefox and safai while javascript is enable?   
Name: AKM
Date: 2010-11-26 7:02:35 AM
Comment:
Work in IE but,why does not work in firefox and safari while javascript is enable on both browser?
Title: Feedback   
Name: Atul Tiwari
Date: 2010-11-22 5:37:58 AM
Comment:
Too good....
Title: A question..   
Name: Dario
Date: 2010-11-14 9:16:49 AM
Comment:
Is it possible for the page method to return a structure instead a simple string?
And how to read it on client side?
Thank you!
Title: Not clear yet   
Name: M
Date: 2010-08-10 6:27:42 AM
Comment:
The article could be better structured. It's all a bit chaotic and confusing...
Title: clear now   
Name: Babak Sekandari
Date: 2010-04-29 9:58:30 AM
Comment:
OK, I understand now.
public static string GetStatus(string s1, string s2){ . . . appears later.
It's better to view the whole article at once.
Very good explanations by the way. Thank you very much.
Title: confused   
Name: Babak Sekandari
Date: 2010-04-28 6:59:47 PM
Comment:
The page method call is: PageMethods.GetStatus(onSucceed, onError);
But the code behind sample is public static string GetPageMethod()

As Woho stated, I don't understand why one is GetPageMethod() and the other is GetStatus()
Title: setting control value   
Name: suresh kumar
Date: 2010-03-26 12:08:31 AM
Comment:
that would be a logical error, have you tried to debug onsucceed method and check the value before it is assigned to textbox. Can you send me the code?
Title: setting control value   
Name: Maulik
Date: 2010-03-25 9:14:31 PM
Comment:
Hi,

OnSucceed event, I want to set the result value to a textbox. For that, I find the textbox and assign result to textbox's "value" property. I receive the expected value for result but textbox has an empty value..any reason?? I use onblur event of another textbox and do the same thing and at that time textbox shows result value..any idea??
Title: Good   
Name: NNN
Date: 2010-03-22 3:19:51 AM
Comment:
Very good article.
Title: GetStatus   
Name: Suresh Kumar
Date: 2010-03-10 4:07:24 AM
Comment:
i am referring to sample method GetStatus in the article.
Title: Interesting, but a bit confusing   
Name: Woho
Date: 2010-03-10 2:33:07 AM
Comment:
You keep referring to the method GetStatus, but I guess you really mean GetPageMethod? Otherwise it doesn't make any sense...
Interesting article although, but feels a bit like "ugly coding" (the concept, not your code :) ).
Title: Good one   
Name: Bijith K B
Date: 2010-03-09 2:04:48 PM
Comment:
Nice article.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-19 4:59:19 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search