简体   繁体   中英

Button onClick Event Handler Never Fires in Chrome Only

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(&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;">

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代码

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(&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;" />

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?

我会用这个来解决它:

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

试试这个...

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM