Geeks With Blogs
Frank Wang's inspirations on .NET
IEnumerable<Inspiration> inspirations = from i in DataContext.Inspirations where i.Sharable == true select i

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: }
Posted on Thursday, March 13, 2008 10:39 PM | Back to top


Comments on this post: ASP.NET AJAX callbacks to Web Methods in ASPX pages

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages
Requesting Gravatar...
Thanks for the write up! Thats money!
Left by Chuck Conway on Mar 23, 2008 10:25 PM

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages
Requesting Gravatar...
Very good! Thanks for the information!
Left by Diego Gazotto Dezembro on Aug 26, 2008 4:49 PM

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages
Requesting Gravatar...
interesting.. is this a good design if you wanted to make an ajaxified server control or would ICallbackEventHandler be better?
Left by Drew on Oct 05, 2008 6:17 AM

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages
Requesting Gravatar...
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.
Left by SBarker on Nov 24, 2008 10:59 AM

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages
Requesting Gravatar...
Very nice Frank!
Left by Andrew Siemer on Jan 23, 2009 2:54 PM

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages
Requesting Gravatar...
Thanks that helped a lot! Here is the Vb code


Left by Abhishek on Feb 25, 2009 7:46 AM

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages
Requesting Gravatar...
Thanks for the article. It helped a lot..
Left by Anish on Apr 14, 2009 3:39 AM

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages
Requesting Gravatar...
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
Left by Mohammad on Aug 31, 2009 4:09 AM

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages
Requesting Gravatar...
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;
Left by Geoff Priestner on Sep 17, 2009 6:02 AM

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages
Requesting Gravatar...
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!
Left by javaChip on Sep 21, 2009 9:26 PM

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages
Requesting Gravatar...
Are there examples in VB instead of C#?
Left by David on Oct 24, 2009 5:01 PM

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages
Requesting Gravatar...
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?
Left by Jack on Oct 27, 2009 12:43 PM

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages
Requesting Gravatar...
i cant use session in static methods ???


Can you help me
Left by omar on Feb 02, 2010 7:33 AM

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages
Requesting Gravatar...
Thank u very much for the article.
Left by mrk on Mar 03, 2010 3:11 AM

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages
Requesting Gravatar...
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
Left by Eddie Silva on Mar 15, 2010 10:23 PM

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages
Requesting Gravatar...
Hey Omar you can use [webMethod(EnableSession=true)]

Left by GG on Aug 19, 2010 5:09 PM

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages
Requesting Gravatar...
Thank you very much
Left by Happy Developer on Dec 17, 2010 4:15 AM

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages
Requesting Gravatar...
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.
Left by Kerry Hormann on Aug 02, 2011 6:30 PM

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages
Requesting Gravatar...
Nice and very simple article to understand about page methods.

Thanks
Kotresha A M S
Left by Kotresha on Sep 27, 2011 2:18 AM

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages
Requesting Gravatar...
Very nice, thanks.
Left by Nikolay on Sep 29, 2011 4:42 AM

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages
Requesting Gravatar...
Thank you!!!!
Left by lxs on Oct 02, 2011 3:11 AM

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages
Requesting Gravatar...
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
Left by MMC on May 16, 2012 10:38 PM

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages
Requesting Gravatar...
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.
Left by Ian Deswarte on May 29, 2012 12:44 PM

# re: ASP.NET AJAX callbacks to Web Methods in ASPX pages
Requesting Gravatar...
What if you are confined to ASP.NET 2.0, can't use AJAX toolkit or Script Manager? How do you do this then?
Left by RJ on Sep 24, 2012 8:07 AM

Your comment:
 (will show your gravatar)


Copyright © Frank Wang | Powered by: GeeksWithBlogs.net | Join free