[英]Calling c# events from javascript code
你可以告诉我是否可以从ASP.NET Web应用程序中的javascript代码调用C#事件或方法?
您无法直接执行C#事件或方法。 您可以通过AJAX命中端点。 这可以是ASP.NET MVC控制器或.ashx处理程序。
或者你可以看看像SignalR这样的东西,它可以通过它的集线器,从javascript(以及服务器,推送通知和那种东西)中做出神奇的事情。
你不能。 C#在服务器端执行,而javascript在客户端(浏览器)执行。
正如我先前指出的那样:
使用Page方法的示例: http : //www.singingeels.com/Articles/Using_Page_Methods_in_ASPNET_AJAX.aspx http://sappidireddy.wordpress.com/2008/03/31/how-to-call-server-side-function-from-客户机侧代码-使用-pagemethods合ASPNET-AJAX /
使用AJAX的示例: http : //www.codeproject.com/Articles/250582/Getting-Started-with-jqChart-HTML5-jQuery-Chart-Pl
是的,可以在asp.net webforms
使用ajax
从javascript
调用C# Method
asp.net webforms
也可以在asp.net MVC
例如,这里是web forms
示例
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
type: "POST",
url: "Default.aspx/GetData",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response.d);
},
failure: function (response) {
alert(response.d);
}
});
});
</script>
和c# method
背后的新代码
[WebMethod]
public static string GetData()
{
return "This string is from Code behind";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.