问题描述
我在这里遇到了一个特殊的问题,我一生都无法弄清楚解决方案是什么.请注意,以下代码不是动态创建的,而是立即在我的 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.
我做错了什么?谢谢
推荐答案
如果您查看生成的源代码,您将看到以下内容:
If you look at the source code generated you will see the following:
onclick="return confirm('Sure?'); __doPostBack('btnSubmit','')"
所以发生的事情是永远不会调用 _doPostBack.做你正在寻找的东西的hacky方法如下:
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:如果使用 onclick,则不会调用 OnServerClick 事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!