gpt4 book ai didi

javascript - 是否有可能在 javascript 中获得 asp.net 'OnClicked' 回调以调用 Web 控件中的函数?

转载 作者:行者123 更新时间:2023-11-29 22:38:35 24 4
gpt4 key购买 nike

这似乎是不可能的,因为回调函数似乎是特定于页面的,但如果可能的话,我想这样做。

最佳答案

您可以调用标有WebMethodstatic 页面方法属性使用 ASP.NET Ajax如果您配置 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.
}
});
}

关于javascript - 是否有可能在 javascript 中获得 asp.net 'OnClicked' 回调以调用 Web 控件中的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4033177/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com