ASP.NET: OnServerClick event handler not called if using onclick(ASP.NET:如果使用 onclick,则不会调用 OnServerClick 事件处理程序)
问题描述
我在这里遇到了一个特殊的问题,我一生都无法弄清楚解决方案是什么.请注意,以下代码不是动态创建的,而是立即在我的 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 事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:ASP.NET:如果使用 onclick,则不会调用 OnServerClick 事


基础教程推荐
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- 获取C#保存对话框的文件路径 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- 将数据集转换为列表 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- 如果条件可以为空 2022-01-01