问题描述
您好,我有这个问题,我试图找到一种方法,从ASP调用控制器的一个JavaScript函数和我在这里做的是code:
<脚本类型=文/ JavaScript的>
你好功能(){
警报(世界你好)
}
< / SCRIPT>
< /头>
<身体GT;
<表ID =form1的=服务器>
< DIV>
< ASP:按钮的ID =buttonme=服务器的OnClientClick =JavaScript的:你好()
文本=点击/>
现在可以说,我的背后有这样的功能code
公用Sub MSG()
MSGBOX(世界你好)
结束小组
和我想从一个javascript函数调用它,所以它会像控制器----背后的javascript调用--- ---> code>
有没有一种方法可以做到这一点。
在Web应用程序中的类定义你的方法的地方。
[System.Web.Services.WebMethod]
公共静态字符串消息()
{
返回的Hello world;
}
添加用的EnablePageMethods脚本经理=真在你的aspx页面
< ASP:的ScriptManager ID =someId=服务器的EnablePageMethods =真>
< / ASP:ScriptManager的>
调用方法在JavaScript
< SCRIPT LANGUAGE =JavaScript的类型=文/ JavaScript的>
PageMethods.Msg(的onSuccess);
功能的onSuccess(响应)
{
警报(响应);
}
< / SCRIPT>
hello i have this question i was trying to find a way to call a javascript function from asp controllers and i did here is the code :
<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" />
now lets say i have this code behind function
Public Sub msg()
MsgBox("hello world")
End Sub
and i want to call it from a javascript function so it will be like controller----call---> javascript ---call--->code behind
is there is a way to do this
Define your method in a class somewhere in your web application.
[System.Web.Services.WebMethod]
public static string Msg()
{
return "Hello world";
}
Add a script manager with EnablePageMethods=true in your aspx page
<asp:ScriptManager ID="someId" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
Call the method in javascript
<script language="javascript" type="text/javascript">
PageMethods.Msg(onSuccess);
function onSuccess(response)
{
alert(response);
}
</script>
这篇关于从JavaScript调用ASP功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!