javascript 如何从javascript触发服务器端事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5924169/
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
How to trigger server-side event from javascript?
提问by Akram Shahda
How can I trigger a server control event from a client-side JavaScript?
如何从客户端 JavaScript 触发服务器控件事件?
回答by Akram Shahda
To call a server side method on a client side event you need to do the following:
要在客户端事件上调用服务器端方法,您需要执行以下操作:
1- Create the server side method:
1-创建服务器端方法:
void DoSomething(...) { ... }
2- Implement the System.Web.UI.IPostBackEventHandler.RaisePostBackEvent
which take one string argument (You can assign the name to the value of this argument).:
2- 实现System.Web.UI.IPostBackEventHandler.RaisePostBackEvent
采用一个字符串参数的方法(您可以将名称分配给该参数的值):
public void RaisePostBackEvent(string eventArgument)
{
DoSomething(...);
}
3- Write a script to trigger post back:
3- 编写一个脚本来触发回发:
function TriggerPostBack(control, arg){
__doPostBack(control, arg);
}
4- Call the PostBack trigger function when needed:
4- 需要时调用 PostBack 触发器函数:
<a .... onclick="TriggerPostBack('control', 'arg')" .. />
回答by Akram Shahda
You can use the following way too:
您也可以使用以下方式:
<a id="myLink" href="#"
onclick="document.getElementById('<%=ServerControl.ClientID%>').Event();
return false;">OK</a>