ASP.NET:OnServerClick事件处理程序不调用如果使用的onclick [英] ASP.NET: OnServerClick event handler not called if using onclick
问题描述
我在这里有一个特殊的问题,我不能用我的生命弄清楚该解决方案是什么。请注意,以下code是的不的动态创建的,只是立刻我的 ASPX
文件。
I have a peculiar problem here and I can't by my life figure out what the solution is. Note that the following code is not dynamically created, but just immediately in my aspx
file.
<button type="button" runat="server" id="btnSubmit"
OnServerClick="btnSubmit_Click" onclick="return confirm('Sure?');">
Submit
</button>
这工作得很好,只要我的不的有的onclick
属性存在,即 OnServerClick
处理程序被解雇,因为它应该。但是,当我使用的onclick
属性它不是,不管我是否确认或拒绝确认对话框。
This works just fine as long as I don't have the onclick
attribute there, i.e. the OnServerClick
handler is fired as it should. But when I use the onclick
attribute it is not, no matter whether I confirm or decline the confirmation dialog box.
我是什么做错了吗?谢谢
What am I doing wrong? Thanks
推荐答案
如果你看一下源$ C $时生成,您将看到以下内容:
If you look at the source code generated you will see the following:
onclick="return confirm('Sure?'); __doPostBack('btnSubmit','')"
所以发生的是_doPostBack永远不会被调用。该哈克的方式做你要找的内容如下:
so what is happening is the _doPostBack is never called. The hacky way to do what you're looking for is the following:
<button type="button" runat="server" id="btnSubmit"
OnServerClick="btnSubmit_Click" onclick="if (confirm('Sure?')) ">
真正正确的方法是使用一个Web控件:
The real correct way would be to use a Web Control:
<asp:Button runat="server"
OnClick="btnSubmit_Click" OnClientClick="return confirm('Sure?')" Text="Submit" />
这篇关于ASP.NET:OnServerClick事件处理程序不调用如果使用的onclick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!