I've just added some JavaScript to the onclick of a Button in ASP.NET to disable the button (in order to prevent the user submitting the form twice).
When I click this button, the page posts back fine but no control event handler runs - ASP.NET seems unable to determine which control posted the page back.
I'm a bit stumped by this unexpected behaviour. Can anyone explain it and suggest an alternative way of doing this?
I've just added some JavaScript to the onclick of a Button in ASP.NET to disable the button (in order to prevent the user submitting the form twice).
When I click this button, the page posts back fine but no control event handler runs - ASP.NET seems unable to determine which control posted the page back.
I'm a bit stumped by this unexpected behaviour. Can anyone explain it and suggest an alternative way of doing this?
If you disable the button before it posts back, the server side event is not registered.
The easier way to handle this is to hide the button after click, or create a handler that keeps the second click from cascading to the form post.
Last time I had this problem using .style.display = "none" in the onclientclick did the trick I needed. Disabled is not just a style, its a state, that the server respects.
<asp:Button ID="Button1" OnClientClick="javascript:this.style.display = 'none'"
onclick="Button1_Click" runat="server" Text="Button" />
Try This:
<asp:Button ID="Button1" UseSubmitBehavior="false"
OnClientClick="javascript:document.getElementById('Button1').disabled = true; __doPostBack('__Page', '');"
onclick="Button1_Click" runat="server" Text="Button" />
It'll e through with no extra request inspection needed.
After your ment about how you add the onclick: Just add _doPostBack('_Page', 'YOURBUTTONNAME') and inspect the event args on the server side.