Frank Wang's inspirations on .NET

IEnumerable<Inspiration> inspirations = from i in DataContext.Inspirations where i.Sharable == true select i

ASP.NET AJAX callbacks to Web Methods in ASPX pages

Thursday, March 13, 2008 10:39 PM

Making ASP.NET AJAX calls to web services has become increasingly popular these days. This approach not only provides a good separation of concerns but also makes it possible to create a truly ajax'ed web site with JavaScript's power on the client side. However, you may in some situations want to move one or more web methods to ASPX pages. You often have methods that are logically dedicated to specific ASPX pages, and you don't really want them to be exposed to the public through the ASMX file. Generally, this is the most appealing reason why you want to place web methods in ASPX pages.

Both ASP.NET AJAX Extensions and ASP.NET 3.5 support AJAX callbacks to the web methods placed in ASPX pages. It only requires a few simple steps to do this. For example, you want to move a web method called GetProducts to an page named Products.aspx, and you want to invoke the GetProducts method directly from JavaScript. The first step is expose GetProducts as a web method.

   1: public partial class Products : System.Web.UI.Page 
   2: { 
   3: [System.Web.Services.WebMethod()] 
   4: [System.Web.Script.Services.ScriptMethod()] 
   5: public static List<Product> GetProducts(int cateogryID) 
   6: {
   7:   // Put your logic here to get the Product list 
   8: }

A few things to note here:

1. The method MUST be static

2. The method needs to be decorated with [WebMethod()]

3. The method needs to be decorated with [ScriptMethod()] if you want to make a ASP.NET AJAX callback to it

Next, remove the reference in the <Services> section of the ScriptManager tag and set the ScriptManager.EnablePageMethods property to true. This will make all the web methods on this page visible to the JavaScript client.

   1: <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />


Finally, we need to change the JavaScript code to call the method through the PageMethods object instead of the Web Service.

   1: function GetProductsByCategoryID(categoryID)
   2: {
   3:     PageMethods.GetProducts(categoryID, OnGetProductsComplete);
   4: }
   5:  
   6: function OnGetProductsComplete(result)
   7: {
   8:     // Put your logic here to display the result
   9: }



Feedback

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages

Thanks for the write up! Thats money! 3/23/2008 10:25 PM | Chuck Conway

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages

Very good! Thanks for the information! 8/26/2008 4:49 PM | Diego Gazotto Dezembro

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages

interesting.. is this a good design if you wanted to make an ajaxified server control or would ICallbackEventHandler be better? 10/5/2008 6:17 AM | Drew

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages

Thanks for the article. I enjoyed reading it. May I ask you opinion on an article or book to read for AJAX design practices. My goal is to create a web site that pulls data the fastest for users. I'm using SQL Server with compiled stored procedures and am looking into a web site that have the usual 1. Main navigation 2. ASPX pages 3. But the pages have a "Left Pane", "Middle Pain" and "Right Pane". I can invision user controls in those panes and only update those panes through either an update panel or callback. I'm willing to write more java script code if the response time to our users is faster than update panels, etc. Any suggested books? Thanks again. 11/24/2008 10:59 AM | SBarker

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages

Very nice Frank! 1/23/2009 2:54 PM | Andrew Siemer

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages

Thanks that helped a lot! Here is the Vb code


2/25/2009 7:46 AM | Abhishek

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages

Thanks for the article. It helped a lot.. 4/14/2009 3:39 AM | Anish

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages

Hey Thanks alot for the post its amazing
iam wondring if i have the script manager in a master page, and an external javascript file for the page
can i still use PageMethods ?

THanks In Advanced 8/31/2009 4:09 AM | Mohammad

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages

If you want to use PageMethods and you have the script manager in a master page then either 1) turn on page methods for all pages by setting EnabledPageMethods to true in the master or 2) set the EnablePageMethods programatically for each 'child' page in its Page_Load or Page_Init:

ScriptManager.GetCurrent(this).EnablePageMethods = true; 9/17/2009 6:02 AM | Geoff Priestner

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages

thanks! this was really helpful..
and also thanks to Geoff..your post helped me as well..having trouble implementing pagemethods for my Child pages..thanks a lot! 9/21/2009 9:26 PM | javaChip

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages

Are there examples in VB instead of C#? 10/24/2009 5:01 PM | David

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages

I am following the instructions and I had it working successfully at one point but now I am trying to use it again and I am not getting the PageMethods in the javascript intellisense whcih always sends up a red flag to me that I have done something wrong.

I have the function in the code behind prefaced with the WebMethod and the ScriptMethod as your example shows, it is defined as static and the datatype is XMLDocument. I added the EneablePageMethods="True" in the ScriptManager tag. but when I go to type in the javascript, it no longer recognizes "PageMethod". Am I missing something? 10/27/2009 12:43 PM | Jack

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages

i cant use session in static methods ???


Can you help me 2/2/2010 7:33 AM | omar

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages

Thank u very much for the article. 3/3/2010 3:11 AM | mrk

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages

Hi,how i get result from webmethod in my page aspx? ex.. i need return only true or false..from my aspx.cs..and from this ...show message.. Thanks 3/15/2010 10:23 PM | Eddie Silva

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages

Hey Omar you can use [webMethod(EnableSession=true)]

8/19/2010 5:09 PM | GG

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages

Thank you very much 12/17/2010 4:15 AM | Happy Developer

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages

Simply brilliant! I don't know why so many others take such a disastrously complex approach when it can be this easy.

I've been searching for this for quite some time. Thank you for this very concise how-to. 8/2/2011 6:30 PM | Kerry Hormann

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages

Nice and very simple article to understand about page methods.

Thanks
Kotresha A M S 9/27/2011 2:18 AM | Kotresha

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages

Very nice, thanks. 9/29/2011 4:42 AM | Nikolay

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages

Thank you!!!! 10/2/2011 3:11 AM | lxs

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages

This error randomly give below error.still user can continue working on the page. the error appears at the left bottom of the internet explorer (like a javascript error). Here Get_CloseWindow is a webmethod.

Message: Sys.Net.WebServiceFailedException: The server method 'Get_CloseWindow' failed with the following error:
Line: 6934
Char: 21
Code: 0
URI: http://lpc/ScriptResource.axd?d=0tVd-vgld34XATxUEJDZ5Kw3SFYCUcKb3tj5MgcbrXXAXJq2tO8KT_ILSfmi5d7gFoTERyHUqC6LothpLuBMTTimxdL4QXPx8Z5tfxFu8NUSfUJCkM2lqk9Ad8NFh6EaMlV5Gcy5z1WO2CL2KWDY86647bfGmNYx2eoFZ8FrZNzcKtNP0&t=4edbeeee
5/16/2012 10:38 PM | MMC

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages

So say you are developing an asp.net ajax CONTROL that uses a callback to the server. Where would you put the [webmethod] code for your control. 5/29/2012 12:44 PM | Ian Deswarte

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages

What if you are confined to ASP.NET 2.0, can't use AJAX toolkit or Script Manager? How do you do this then? 9/24/2012 8:07 AM | RJ

Post a comment