2013-05-27 72 views
10

我想从JS调用静态服务器端方法,所以我决定在我的网站上使用ScriptManager控件。 所以我有一个母版页,这样的结构:asp.net ScriptManager PageMethods未定义

<%@ 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中使用它。

回答

2

我已经明白为什么PageMethod的对象是undefinded,因为ScriptManager的组件从旁边放置使用PageMethod的脚本,因此当页面呈现并执行脚本时,此时没有PageMethod。所以我需要在按钮单击或窗口加载事件时调用getGiftFileUrl(),当页面上的所有脚本都可以使用时。

+2

getGiftFileUrl()的实际调用任何带解决方案的最终示例?或标记@vitorcanova答案。 – Kiquenet

21

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

奖励:既然是你不能使用this.Session["mySessionKey"]一个static方法,但你可以做HttpContext.Current.Session["mySessionKey"]

+0

也许WebMethod-PageMethods和JSON。 – Kiquenet

+0

这应该是被接受的答案 – Fandango68

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

您错过了ASP控件 – Fandango68

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

将100%的工作。