admin管理员组文章数量:1291620
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" />
In the .aspx.cs page, I have the following:
protected void myButton_Click(object sender, EventArgs e)
{
// Do Stuff
}
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>
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 municating what I'm trying to acplish. Can someone please tell me how to do this?
Thank you so much!
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" />
In the .aspx.cs page, I have the following:
protected void myButton_Click(object sender, EventArgs e)
{
// Do Stuff
}
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>
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 municating what I'm trying to acplish. Can someone please tell me how to do this?
Thank you so much!
Share Improve this question edited May 8, 2012 at 13:08 Crab Bucket 6,2779 gold badges41 silver badges73 bronze badges asked Jan 30, 2012 at 16:36 Mono DeveloperMono Developer 6192 gold badges7 silver badges22 bronze badges3 Answers
Reset to default 8You could write in
__doPostBack('<%=myButton.ClientID%>','OnClick');
or a bit better and less error prone use the ASP.Net ClientScriptManager
<%= Page.ClientScript.GetPostBackEventReference(myButton, String.Empty) %>;
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.
This recent SO question explains it all far better than I can (or rather did at the time)
try this:
<input type="button" id="myButton" onclick="someJSFunction(); __doPostBack('myButton','OnClick');" />
First of all, whatever your button is doing, the actual code to do that function shouldn't be in the code behind the button; it should be in a method that is called by the code behind the button. The button's event handlers should never be used directly by any object other than the button. So your button handler should look like this:
protected void Button1_Click(object sender, EventArgs e)
{
CallMyRealMethod();
}
Next, check out SignalR. (http://signalr) SignalR (in a nutshell) allows you to call C# methods from your javascript, and javascript methods from C#.
Using these two techniques can make your code much more readable and maintainable.
本文标签: cCalling an ASPNET EventHandler from JavaScriptStack Overflow
版权声明:本文标题:c# - Calling an ASP.NET EventHandler from JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741538740a2384187.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论