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.
- The page method must have the System.Web.Services.WebMethod attribute.
[WebMethod]
... - The page method must be public.
[WebMethod]
public ... - The page method must be static.
[WebMethod]
public static ... - 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 ... - 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. - [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.