C#中的javascript函数? [英] C# in javascript function?
本文介绍了C#中的javascript函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我怎么能调用一个函数C#(服务器端)的JavaScript函数(客户端),触发浏览器功能被关闭?如:
window.onbeforeclose = MyFunction的()// C#
解决方案
您不能调用从客户端服务器端功能,您将需要Web方法的属性来装饰你的C#方法,使之静把它公开为AJAX调用成员:
像
[的WebMethod]
公共静态无效someCSharpFunction(){
//的函数体
}
和在你的客户端
$。阿贾克斯({
键入:POST,
网址:SomePage.aspx页面/ someCSharpFunction
数据:{},
的contentType:应用/ JSON的;字符集= UTF-8,
数据类型:JSON
成功:函数(MSG){
//做一些有趣的事情在这里。
}
});
希望有所帮助。
How can I call a function C# (server side) function in a Javascript function (client side) which triggers the browser is closed? as:
window.onbeforeclose = MyFunction() // C#
解决方案
You can not call your server side function from client side , You will need to decorate your C# method with Web Method attribute and make it static to expose it as a callable AJAX member: something like
[WebMethod]
public static void someCSharpFunction() {
// body of function
}
and in your client side
$.ajax({
type: "POST",
url: "SomePage.aspx/someCSharpFunction",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// Do something interesting here.
}
});
Hope that helps.
这篇关于C#中的javascript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文