PageMethods Not Found (or Defined)?

If you are seeing this javascript error (or similar) while trying to use Page Methods with ASP.NET AJAX, here is a checklist that should save you some time.

  1. The page method must have the System.Web.Services.WebMethod attribute.
    [WebMethod]
    ...
  2. The page method must be public.
    [WebMethod]
    public ...
  3. The page method must be static.
    [WebMethod]
    public static ...
  4. The page method must be defined on the page (either inline or in the code-behind).  It cannot be defined in a control, master page, or base page.
    <%@ Page Language="C#" %>
    <script runat="server">
    [WebMethod]
    public static ...
  5. The ASP.NET AJAX Script Manager must have EnablePageMethods set to true.
    <asp:ScriptManager EnablePageMethods="true" For an example with full source see Calling static methods in an ASP.NET Web Page.
  6. [Reader Tip] If all else fails, be sure you are seeing the correct error.  A reader kindly reported that although they saw the "PageMethods not defined" error, the server-side code was still executing correctly.  After switching to FireFox, they discovered the real error.  Perhaps Firebug would also be quite useful while tracking down such scandalous errors. [Thank JohnL for the tip] 
    Note that you can use complex return types and parameters with page methods the same as with calling web services with ASP.NET AJAX.
posted @ 2010-04-23 11:02  KevinWang  阅读(380)  评论(0编辑  收藏  举报