如何从客户端调用代码隐藏函数?
// Code-behind function:
public void CodeBehindFunction(int i)
{
...
}我想从客户端调用它...
// Client-side call:
$(this). ?? 发布于 2011-12-07 18:02:18
您的意思是,您希望从客户端代码调用服务器端例程...
我建议您使用JavaScript:__doPostback('CodeBehindFunction',iParameter);强制回发,然后在服务器端代码的Page Init事件中,通过如下方式捕获此自定义回发。
if (Request.Params["__EVENTTARGET"] == "CodeBehindFunction")
{
int i;
// Try to convert the argument (in the example this is the value of iParameter)
if (int.TryParse(Request.Params("__EVENTARGUMENT"),out i)
{
CodeBehindFunction(i);
}
}或者,使用通过jQuery.ajax调用的WebService。
发布于 2011-12-07 18:31:43
您可以使用ajax来调用CodeBehindFunction(int ),但您必须将该方法标记为webmethod和static:
[WebMethod]
public static void CodeBehindFunction(int i)
{
...
}然后从js调用它。
$.ajax({
type: "POST",
url: "/yourPage.aspx/CodeBehindFunction",
data: "{'i':'"+num+"'}", //PUT DATA HERE
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
}
})** yourPage.aspx是声明CodeBehindFunction方法的页面
https://stackoverflow.com/questions/8413400
复制相似问题