2
看起來好像不可能,因爲回調函數似乎是頁面特定的,但如果可能的話我想這樣做。是否有可能在javascript中調用asp.net'OnClicked'回調來調用Web控件中的函數?
看起來好像不可能,因爲回調函數似乎是頁面特定的,但如果可能的話我想這樣做。是否有可能在javascript中調用asp.net'OnClicked'回調來調用Web控件中的函數?
可以調用標有使用ASP.NET Ajax的WebMethod屬性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.
}
});
}