問(wèn)題描述
我在這里遇到了一個(gè)特殊的問(wèn)題,我一生都無(wú)法弄清楚解決方案是什么.請(qǐng)注意,以下代碼不是動(dòng)態(tài)創(chuàng)建的,而是立即在我的 aspx
文件中創(chuàng)建的.
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
處理程序按應(yīng)有的方式被觸發(fā).但是當(dāng)我使用 onclick
屬性時(shí),無(wú)論我是確認(rèn)還是拒絕確認(rèn)對(duì)話框,它都不是.
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.
我做錯(cuò)了什么?謝謝
推薦答案
如果您查看生成的源代碼,您將看到以下內(nèi)容:
If you look at the source code generated you will see the following:
onclick="return confirm('Sure?'); __doPostBack('btnSubmit','')"
所以發(fā)生的事情是永遠(yuǎn)不會(huì)調(diào)用 _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" />
這篇關(guān)于ASP.NET:如果使用 onclick,則不會(huì)調(diào)用 OnServerClick 事件處理程序的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!