2010-10-27 56 views

回答

2

可以調用標有使用ASP.NET AjaxWebMethod屬性static頁方法,如果你配置ScriptManager這樣做:

<form id="form" runat="server"> 
    <asp:ScriptManager ID="ScriptManager" runat="server" 
     EnablePageMethods="true" /> 
    . 
    . 
    . 
</form> 

[WebMethod] 
public static int Foo(string bar) 
{ 
    return 42; 
} 

然後在您的客戶端代碼:

function callFoo(bar) 
{ 
    return PageMethods.Foo(bar); 
} 

你可以也做同樣的with jQuery

function callFoo(bar) 
{ 
    $.ajax({ 
     type: "POST", 
     url: "YourPage.aspx/Foo", 
     data: { 
      "bar": bar 
     }, 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function(message) { 
      // Do something. 
     } 
    }); 
} 
相關問題