asp.net ScriptManager PageMethods is undefined
Asked Answered
O

4

11

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.

Overscrupulous answered 27/5, 2013 at 14:13 Comment(0)
O
2

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.

Overscrupulous answered 28/5, 2013 at 8:18 Comment(1)
any final sample with solution ? or marked @vitorcanova answer.Secunda
J
24

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

Jellicoe answered 27/5, 2013 at 16:10 Comment(1)
Maybe WebMethod-PageMethods and JSON.Secunda
W
3

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>
Wennerholn answered 27/5, 2013 at 16:24 Comment(1)
You're missing the actual call to getGiftFileUrl() from the ASP controlEmbarrassment
O
2

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.

Overscrupulous answered 28/5, 2013 at 8:18 Comment(1)
any final sample with solution ? or marked @vitorcanova answer.Secunda
G
-4
 <script type="text/javascript">
       function Generate()
       {              
           var result = PageMethods.GenerateOTP(your parameter, function (response)
           {
               alert(response);
           });
       }
</script>

Will 100% work.

Gehring answered 20/12, 2013 at 6:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.