从JavaScript调用一个ASP.NET事件处理程序 [英] Calling an ASP.NET EventHandler from JavaScript
问题描述
我有一个传统的ASP.NET Web窗体。我们曾经有过定义一个按钮,如下:
I have a traditional ASP.NET Web Form. We used to have a button defined as follows:
<asp:Button ID="myButton" Runat="server" OnClick="myButton_Click" />
在.aspx.cs页,我有以下几点:
In the .aspx.cs page, I have the following:
protected void myButton_Click(object sender, EventArgs e)
{
// Do Stuff
}
不过,现在有人已经决定使用一个奇特的JavaScript框架。从本质上讲,我只是有:
However, now someone has decided to use a fancy JavaScript framework. Essentially, I just have:
<input type="button" id="myButton" onclick="someJSFunction()" />
<script type="text/javascript">
function someJSFunction() {
// Need to execute "myButton_Click" here.
}
</script>
我如何真正从服务器上执行在这里我原来的方法是什么?我知道这是一个很奇怪的问题。我已经简化了code尝试,并在沟通什么,我试图完成一个直接的。有人可以告诉我如何做到这一点?
How do I actually execute my original method on the server from here? I know this is a very bizarre question. I've simplified the code to try to and be a direct in communicating what I'm trying to accomplish. Can someone please tell me how to do this?
感谢你了!
推荐答案
您可以写在
__doPostBack('<%=myButton.ClientID%>','OnClick');
或好一点且不易出错使用ASP.Net ClientScriptManager
or a bit better and less error prone use the ASP.Net ClientScriptManager
<%= Page.ClientScript.GetPostBackEventReference(myButton, String.Empty) %>;
第二个实际上写 __ doSubmit
在幕后,但你得到的ASP.Net框架来为你做它。同时日EFIRST一会回来后的页面,但第二个触发正确的服务器端事件也是如此。它集成了很多更好的进入ASP.Net架构。要么会的工作,虽然。
The second actually writes __doSubmit
under the covers but you are getting the ASP.Net framework to do it for you. Also th efirst one will post back the page but the second triggers the correct server side events as well. It integrates a lot better into the ASP.Net architecture. Either would work though.
这最近的<一个href=\"http://stackoverflow.com/questions/8771097/raise-server-side-button-click-event-from-javascript-in-ajax-call/8771155#8771155\">SO问题解释这一切远不如我可以(或者说做的时候)
This recent SO question explains it all far better than I can (or rather did at the time)
这篇关于从JavaScript调用一个ASP.NET事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!