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.

4

4 回答 4

24

要使用 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"]

于 2013-05-27T16:10:22.110 回答
3

在您的代码隐藏中创建此方法:

[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>
于 2013-05-27T16:24:00.793 回答
2

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

于 2013-05-28T08:18:02.520 回答
-4
 <script type="text/javascript">
       function Generate()
       {              
           var result = PageMethods.GenerateOTP(your parameter, function (response)
           {
               alert(response);
           });
       }
</script>

将 100% 工作。

于 2013-12-20T06:02:42.170 回答