按钮 onClick 事件处理程序永远不会仅在 Chrome 中触发
我正在开发一个旧的 ASP.NET WebForms 应用程序,该应用程序有一个带有以下控件的 .aspx 页面:
<asp:Button ID="Budget_Approve" OnClick="Budget_Approve_Click" runat="server"
Visible="True" Width="100" Height="30" Text="Approve"></asp:Button>
Budget_Approve_Click
事件处理程序从未被命中,我正在尝试确定原因。我注意到,当页面加载时,会执行此代码以向 onclick
属性添加一些内联 js:
Budget_Approve.Attributes.Add("onclick", "return confirm_approve();");
呈现的 HTML:
<input type="submit" name="ctl00$mainContent$Budget_Approve" value="Approve"
onclick="return confirm_approve();WebForm_DoPostBackWithOptions(new
WebForm_PostBackOptions("ctl00$mainContent$Budget_Approve",
"", true, "", "", false, false))"
id="ctl00_mainContent_Budget_Approve" style="height:30px;width:100px;">
因此,当我单击时,我期望 confirm_approve()
待执行。如果它返回true
,我期望回发并触发我的事件处理程序。在 Chrome 中调试,我发现 confirm_approve()
确实返回 true
:
但是,回发永远不会发生,并且 Budget_Approve_Click
事件处理程序永远不会被命中。为什么不呢?
编辑:我尝试删除完全添加内联 JavaScript 代码的行。然而,仍然没有回发。为该按钮呈现以下 HTML:
<input type="submit" name="ctl00$mainContent$Budget_Approve"
value="Approve"
onclick="javascript:WebForm_DoPostBackWithOptions(new
WebForm_PostBackOptions("ctl00$mainContent$Budget_Approve",
"", true, "", "", false, false))"
id="ctl00_mainContent_Budget_Approve" style="height:30px;width:100px;" />
更新: 发现回发在 IE 中有效,但在 Chrome 中仍然无效。是否有任何特定于浏览器的设置或问题可能会导致此问题?
I am working on an old ASP.NET WebForms application that has an .aspx page with the following control:
<asp:Button ID="Budget_Approve" OnClick="Budget_Approve_Click" runat="server"
Visible="True" Width="100" Height="30" Text="Approve"></asp:Button>
The Budget_Approve_Click
event handler is never being hit, and I am trying to determine why. I noticed when the page loads, this code gets executed to add some inline js to the onclick
attribute:
Budget_Approve.Attributes.Add("onclick", "return confirm_approve();");
The HTML that gets rendered:
<input type="submit" name="ctl00$mainContent$Budget_Approve" value="Approve"
onclick="return confirm_approve();WebForm_DoPostBackWithOptions(new
WebForm_PostBackOptions("ctl00$mainContent$Budget_Approve",
"", true, "", "", false, false))"
id="ctl00_mainContent_Budget_Approve" style="height:30px;width:100px;">
So when I click, I expect confirm_approve()
to be executed. If it returns true
, I expect a postback and for my event handler to fire. Debugging in Chrome, I find that confirm_approve()
does indeed return true
:
However, the postback never happens, and the Budget_Approve_Click
event handler never gets hit. Why not?
Edit: I tried removing the line that adds the inline javascript code entirely. However, still no postback. The following HTML is rendered for the button:
<input type="submit" name="ctl00$mainContent$Budget_Approve"
value="Approve"
onclick="javascript:WebForm_DoPostBackWithOptions(new
WebForm_PostBackOptions("ctl00$mainContent$Budget_Approve",
"", true, "", "", false, false))"
id="ctl00_mainContent_Budget_Approve" style="height:30px;width:100px;" />
Update: Discovered that the postback does work in IE, but still not Chrome. Are there any browser-specific settings or issues that could potentially cause this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会用这个来解决它:
I would just work around it with this:
试试这个...
Try this...
检查您的按钮是否以某种方式被禁用。
$("input[type='提交']").attr("已禁用", "已禁用");
如果发生类似情况,Chrome 将收到不完整的 POST 请求。
ASP.NET 不会触发服务器端事件。
Check if your button is disabled somehow.
$("input[type='submit']").attr("disabled", "disabled");
If something like this happens, Chrome will have an incomplete POST request.
ASP.NET will not fire the server side events.