2013-05-27 71 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%的工作。