PageMethods Is Undefined ASP.NET AJAX

If you are getting PageMethods Is Undefined Error while using AJAX in ASP.NET or using a webservice than you need to check these things to resolve the issue

1. PageMethods needs to be static
[WebMethod]
public static int MethodName()
{
}


2. Enable pageMethods in scriptManager

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

3. add reference to script service in code behind
Add [Microsoft.Web.Script.Services.ScriptMethod] or [System.Web.Script.Services.ScriptService] in code behind below [WebMethod]

WebMethod] 
[Microsoft.Web.Script.Services.ScriptMethod]
public static string GetRecords()  
{  
//DO something  
}



Hope this helps

8 comments:

  1. Add script manager attribute as EnablePartialRendering="true".

    ReplyDelete
  2. i'm using webusercontrol. i want to call C# function in this usercontrol, but the scriptmanager located in masterpage.

    ReplyDelete
  3. Thanks, I used to keep getting "PageMethods is undefined"... then i changed my method to be static and it works now :)

    cheers!

    ReplyDelete
  4. I think there is a bracket missing in #3.

    ReplyDelete
  5. I wan to handle event arguments in the above webmethod. How can i handle paramater based web methods? Please help me

    ReplyDelete