asp.net ScriptManager PageMethods is undefined

I want to call static server-side methods from JS so i decide to use ScriptManager control on my site. So i have a master page, with such structure:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="TopLevelMasterPage.Master.cs"
    Inherits="Likedrive.MasterPages.TopLevelMasterPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:fb="http://ogp.me/ns/fb#">

<head runat="server">
    <title></title>
        <script type="text/javascript">
            function getGiftFileUrl() {
                function OnSuccess(response) {
                    alert(response);
                }
                function OnError(error) {
                    alert(error);
                }

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

            getGiftFileUrl();

        </script>
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManagerMain"
            runat="server"
            EnablePageMethods="true" 
            ScriptMode="Release" 
            LoadScriptsBeforeUI="true">
    </asp:ScriptManager>
    <asp:ContentPlaceHolder ID="MainContent" runat="server"> 
    </asp:ContentPlaceHolder>
    </form>
</body>
</html>

But when page is loading, i have a JS exception - PageMethods is undefined. I supposed that object will be created implicit so i can use it in my javascript.


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"].


In your codebehind create this method:

[WebMethod]
public static void GetGiftFileUrl(string value1, int value2, int value3)
{
    // Do Stuff
}

your js script should resemble this too:

<script type="text/javascript">
    function getGiftFileUrl() {
        PageMethods.GetGiftFileUrl("hero", 1024, 768, OnSucceeded, OnFailed);
    }

    function OnSucceeded(response) {
        alert(response);
    }
    function OnFailed(error) {
        alert(error);
    }


    getGiftFileUrl();
</script>