5

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 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(&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?

3 Answers 3

3

I would just work around it with this:

Budget_Approve.Attributes.Add("onclick", "if (!confirm_approve()) return false;");
5
  • thx for the input. tried it, but still nothing. i even tried removing this line entirely, but no change in results. seems there may be a bigger issue.. Jan 20, 2012 at 18:54
  • @mhornfeck - well what html was generated? Can you edit it back into your answer Jan 20, 2012 at 18:56
  • posted results to original question Jan 20, 2012 at 19:06
  • @mhornfeck - so you're no longer messing with the button at all, and no postback? Odd. Have you checked your console for errors? Jan 20, 2012 at 19:10
  • No errors in the console. But, I tried running the page in IE (instead of Chrome) and it does postback. Now, to find an explanation for that.. Jan 20, 2012 at 19:16
1

Try this...

Budget_Approve.Attributes.Add("onclick", "if (!confirm_approve()) return false;");
4
  • It's exactly the same as the @Adam Racki's answer.
    – Lion
    Jan 20, 2012 at 17:50
  • 1
    They must have been typing at the same time Jan 20, 2012 at 17:51
  • We were off by a minute - he didn't copy me Jan 20, 2012 at 17:53
  • Yes it may be. I don't offend @Naeem Sarfraz but just somewhat curious!
    – Lion
    Jan 20, 2012 at 17:55
-1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.