按钮 onClick 事件处理程序永远不会仅在 Chrome 中触发

发布于 2024-12-27 21:03:19 字数 1809 浏览 5 评论 0原文

我正在开发一个旧的 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(&quot;ctl00$mainContent$Budget_Approve&quot;, 
&quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, false))"
id="ctl00_mainContent_Budget_Approve" style="height:30px;width:100px;">

因此,当我单击时,我期望 confirm_approve() 待执行。如果它返回true,我期望回发并触发我的事件处理程序。在 Chrome 中调试,我发现 confirm_approve() 确实返回 true

javascript code

但是,回发永远不会发生,并且 Budget_Approve_Click 事件处理程序永远不会被命中。为什么不呢?

编辑:我尝试删除完全添加内联 JavaScript 代码的行。然而,仍然没有回发。为该按钮呈现以下 HTML:

<input type="submit" name="ctl00$mainContent$Budget_Approve" 
value="Approve"
onclick="javascript:WebForm_DoPostBackWithOptions(new
WebForm_PostBackOptions(&quot;ctl00$mainContent$Budget_Approve&quot;,
&quot;&quot;, true, &quot;&quot;, &quot;&quot;, 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:

javascript code

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北恋 2025-01-03 21:03:19

我会用这个来解决它:

Budget_Approve.Attributes.Add("onclick", "if (!confirm_approve()) return false;");

I would just work around it with this:

Budget_Approve.Attributes.Add("onclick", "if (!confirm_approve()) return false;");
素手挽清风 2025-01-03 21:03:19

试试这个...

Budget_Approve.Attributes.Add("onclick", "if (!confirm_approve()) return false;");

Try this...

Budget_Approve.Attributes.Add("onclick", "if (!confirm_approve()) return false;");
兲鉂ぱ嘚淚 2025-01-03 21:03:19

检查您的按钮是否以某种方式被禁用。

$("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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文