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.
Filed under: ,

Leave a Comment

Comments

  • Talespinner said:

    I have mine defined on the code-behind, and it seems to work fine.

    02 Apr, 2007 @ 06:33 PM
  • Kyle said:

    You're right Tailspinner.  #4 was a bit misleading.  I've updated it to clarify.

    Thanks,

    Kyle

    02 Apr, 2007 @ 07:43 PM