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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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>

Method 3

I’ve realize why the PageMethod object was undefinded, because ScriptManager component placed next from the script that uses PageMethod, so when page is rendered and script executed, there is no PageMethod at this moment. So i need to call getGiftFileUrl() on button click or on window load event, when all scripts on page are ready to use.

Method 4

 <script type="text/javascript">
       function Generate()
       {              
           var result = PageMethods.GenerateOTP(your parameter, function (response)
           {
               alert(response);
           });
       }
</script>

Will 100% work.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x