你好,我有这个问题,我试图找到一种从 asp Controller 调用 javascript 函数的方法,我在这里做的是代码:
<script type="text/javascript">
function hello() {
alert("hello world")
}
</script>
< /head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="buttonme" runat="server" OnClientClick="javascript:hello()"
Text="click" />
现在假设我有这个代码隐藏功能
Public Sub msg()
MsgBox("hello world")
End Sub
我想从 javascript 函数中调用它,所以它就像 Controller ----调用---> javascript ---调用--->代码隐藏
有没有办法做到这一点
请您参考如下方法:
在您的 Web 应用程序某处的类中定义您的方法。
[System.Web.Services.WebMethod]
public static string Msg()
{
return "Hello world";
}
在您的 aspx 页面中使用 EnablePageMethods=true 添加脚本管理器
<asp:ScriptManager ID="someId" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
在javascript中调用方法
<script language="javascript" type="text/javascript">
PageMethods.Msg(onSuccess);
function onSuccess(response)
{
alert(response);
}
</script>
