ASP.NET ScriptManager PageMethods 未定义。

11

我想从JS调用静态的服务器端方法,所以我决定在我的网站上使用ScriptManager控件。因此,我有一个带有以下结构的母版页:

<%@ 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>
但是当页面加载时,我遇到了JS异常 - PageMethods未定义。我认为对象应该隐式创建,以便我可以在我的javascript中使用它。
4个回答

24

要使用PageMethods,您需要按照以下步骤操作:

  1. 您需要使用 ScriptManager 并设置 EnablePageMethods。(您已经这样做了)。
  2. 在代码后台中创建一个 static 方法,并使用 [WebMethod] 特性。
  3. 像在C#中一样调用javascript中的方法,但是您需要填写更多的参数,包括 sucesserror 回调函数。(您已经这样做了)。

您是否漏掉了其中任何一步?

编辑: 刚才意识到您已经进行了此操作:

            function getGiftFileUrl() {
            function OnSuccess...

您的回调函数是在另一个函数内部定义的。您需要像这样定义回调函数:

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

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

而您的后端代码可能会以类似于以下内容结束:

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

奖励: 因为这是一个静态方法,所以您不能使用this.Session["mySessionKey"],但您可以使用HttpContext.Current.Session["mySessionKey"]


可能是 WebMethod-PageMethods 和 JSON。 - Kiquenet

3
在您的代码后台创建此方法:
[WebMethod]
public static void GetGiftFileUrl(string value1, int value2, int value3)
{
    // Do Stuff
}

你的JavaScript脚本也应该类似于这样:

<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>

你错过了从ASP控件调用getGiftFileUrl()的实际调用。 - Fandango68

2
我意识到为什么PageMethod对象未被定义,因为ScriptManager组件紧挨着使用PageMethod的脚本,所以当页面呈现并执行脚本时,此时没有PageMethod。因此,我需要在按钮单击或窗口加载事件上调用getGiftFileUrl(),当页面上的所有脚本都准备好使用时。

2
有最终的样例和解决方案吗?或者标记@vitorcanova的答案。 - Kiquenet

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

一定会有效。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接