asp.net ScriptManager PageMethods is undefined

To use PageMethods you need to follow these steps:

  1. You need to use ScriptManager and set EnablePageMethods. (You did).
  2. Create a static method in your code behind and use the [WebMethod] attribute.
  3. Call you method in javascript like you should do in C# but you have more parameter do fill, the sucess and error callbacks. (You did).

Did you miss any of these steps?

Edit:
Just realized you did this:

            function getGiftFileUrl() {
            function OnSuccess...

You have yours callbacks inside a function. You need yours callbacks like this:

            function OnSuccess(response) {
               alert(response);
            }
            function OnError(error) {
                alert(error);
            }

PageMethods.GetGiftFileUrl("hero", 1024, 768, OnSuccess, OnError);

And you code behind probably will end in something like that:

[WebMethod]
public static string GetGiftFileUrl(string name, int width, int height)
{
    //... work
    return "the url you expected";
}

Bonus: Since it is a static method you can’t use this.Session["mySessionKey"], but you can do HttpContext.Current.Session["mySessionKey"].

Leave a Comment