从 JavaScript 调用 ASP.NET 代码隐藏函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2521352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling ASP.NET Code Behind function from JavaScript
提问by Satyaprakash J
Is it possible to call ASP.NET codebehind function from JavaScript.
是否可以从 JavaScript 调用 ASP.NET 代码隐藏函数。
回答by Muhammad Akhtar
yes, you can make web method like..
是的,您可以制作网络方法,例如..
<WebMethod(EnableSession:=True), ScriptMethod()> _
Public Shared Function updateContent() As String
Return "Your String"
End Function
and then call in javascript like..
然后像 javascript 一样调用..
PageMethods.updateTabContent(parameterValueIfAny, onSuccessMethod,onFailMethod);
this also need to add
这也需要添加
<asp:ScriptManager ID="ScriptMgr" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
回答by Andre Kraemer
I would prefer Muhammad Akhtar PageMethod approach. Just one short note: You don't need the scriptmanager. The scriptmanager only generates the javascript proxy methods for you. If you already have JQuery on your page, you can forget about the scriptmanager and write something like this on your page instead:
我更喜欢 Muhammad Akhtar PageMethod 方法。只是一个简短的说明:您不需要脚本管理器。脚本管理器只为您生成 javascript 代理方法。如果您的页面上已经有 JQuery,您可以忘记脚本管理器并在您的页面上编写如下内容:
<script type ="text/javascript">
$(document).ready(function() {
$("#AjaxLink").click(function(e) {
e.preventDefault();
$.ajax({
type: "POST",
url: "YourPage.aspx/updateContent",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(result) {
$("#content").html(result.d);
}
});
});
});
</script>
this assumes that you have a link with the ID AjaxLink on your page as well as a div with the id content that shows the result. The benefit is that you save 30kb javascript compared between jquery and the injected scripts by the scriptmanager
这假设您的页面上有一个带有 ID AjaxLink 的链接,以及一个带有显示结果的 ID 内容的 div。好处是,与 jquery 和脚本管理器注入的脚本相比,您可以节省 30kb javascript
回答by jithin
cs function
cs函数
[System.Web.Services.WebMethod]
public static string set_single_amount(arg a1)
{
return value;
}
in script cs fucnction calling using scriptmanager
在脚本中使用脚本管理器调用 cs 功能
$("#control").click(function()
{
PageMethods.set_single_amount(value,afercalling)
});
function afercalling(result)
{
alert(result);
}
回答by Franci Penov
No, it is not possible to call ASP.NET code behind function from Javascript directly. ASP.NET code behind executes on the server in the context of the ASP.NET worker process. Javascript is executed on the client in the context of the client's browser.
不,不能直接从 Javascript 调用函数背后的 ASP.NET 代码。后面的 ASP.NET 代码在 ASP.NET 工作进程的上下文中在服务器上执行。Javascript 在客户端浏览器的上下文中在客户端上执行。
The only way Javascript could trigger execution of ASP.NET code behind is through making an AJAX call from the Javascript to the server.
Javascript 可以触发后台执行 ASP.NET 代码的唯一方法是通过从 Javascript 到服务器进行 AJAX 调用。
回答by rahul
You can make use of __doPostBackto make a postback from javascript. Just add a server control with AutoPostBackproperty and set it to true.
您可以利用__doPostBack来从 javascript 进行回发。只需添加一个具有AutoPostBack属性的服务器控件并将其设置为 true。
回答by prashanth
If you are working on a non-Ajax project, you can try System.Web.UI.ICallbackEventHandler.
如果您正在处理非 Ajax 项目,则可以尝试 System.Web.UI.ICallbackEventHandler。
Samples here:
样品在这里:
http://msdn.microsoft.com/en-us/library/ms178210.aspx
http://www.ajaxmatters.com/2006/05/using-icallbackeventhandler-in-asp-net/
http://msdn.microsoft.com/en-us/library/ms178210.aspx
http://www.ajaxmatters.com/2006/05/using-icallbackeventhandler-in-asp-net/

