【问题标题】:asp.net ScriptManager PageMethods is undefinedasp.net ScriptManager PageMethods 未定义
【发布时间】:2013-05-22 10:11:45
【问题描述】:

我想从 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 中使用它。

【问题讨论】:

    标签: javascript asp.net ajax scriptmanager


    【解决方案1】:

    要使用 PageMethods,您需要执行以下步骤:

    1. 您需要使用ScriptManager 并设置EnablePageMethods。 (你做到了)。
    2. 在后面的代码中创建一个static 方法并使用[WebMethod] 属性。
    3. 在 javascript 中调用你的方法,就像你在 C# 中应该做的那样,但是你有更多的参数来填充,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";
    }
    

    奖励:由于它是static 方法,因此您不能使用this.Session["mySessionKey"],但可以使用HttpContext.Current.Session["mySessionKey"]

    【讨论】:

    • 可能是 WebMethod-PageMethods 和 JSON。
    【解决方案2】:

    在你的代码隐藏中创建这个方法:

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

    你的 js 脚本也应该是这样的:

    <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() 的实际调用
    【解决方案3】:

    我已经意识到为什么PageMethod对象是未定义的,因为ScriptManager组件放置在使用PageMethod的脚本的下一个,所以当页面被渲染并执行脚本时,此时没有PageMethod。因此,当页面上的所有脚本都准备好使用时,我需要在按钮单击或窗口加载事件上调用 getGiftFileUrl()。

    【讨论】:

    • 任何带有溶液的最终样品?或标记为@vitorcanova 答案。
    【解决方案4】:
     <script type="text/javascript">
           function Generate()
           {              
               var result = PageMethods.GenerateOTP(your parameter, function (response)
               {
                   alert(response);
               });
           }
    </script>
    

    将 100% 工作。

    【讨论】:

      猜你喜欢
      • 2013-11-04
      • 1970-01-01
      • 2015-08-14
      • 2014-04-21
      • 2013-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多