Like every other web developer on the planet, I have an issue with users double clicking the submit button on my forms. My understanding is that the conventional way to han
"Disabling" HTML controls doesn't always produce consistent behavior in all major browsers. So I try to stay away from doing that on the client-side, because (working with the ASP.NET model) you need to keep track of element's state on client and server in that case.
What I'd do is move button off the visible part of the window by switching the button's className to a CSS class that contains the following:
.hiddenButton
{
position: absolute;
top: -1000px;
left: -1000px;
}
Now, what to put in place of the button?
And this can be done the same way but in reverse. Start with the element being hidden at page load and then switch to a visible className on form submit.
UseSubmitBehavior="false"
converts submit button to normal button (<input type="button">
). If you don't want this to happen, you can hide submit button and immediately insert disabled button on its place. Because this happens so quickly it will look as button becoming disabled to user. Details are at the blog of Josh Stodola.
Code example (jQuery):
$("#<%= btnSubmit.ClientID %>").click(function()
{
$(this)
.hide()
.after('<input type="button" value="Please Wait..." disabled="disabled" />');
});
FOR JQUERY USERS
You will get into all sorts of problems trying to add javascript directly to the onClick event on ASP.NET buttons when using jQuery event listeners.
I found the best way to disable buttons and get the postback to work was to do something like this:
$(buttonID).bind('click', function (e) {
if (ValidateForm(e)) {
//client side validation ok!
//disable the button:
$(buttonID).attr("disabled", true);
//force a postback:
try {
__doPostBack($(buttonID).attr("name"), "");
return true;
} catch (err) {
return true;
}
}
//client side validation failed!
return false;
});
Where ValidateForm is your custom validation function which returns true or false if your form validates client side.
And buttonID is the id of your button such as '#button1'
fallen888 is right, your approach doesn't work cross-browser. I use this little snippet to prevent double-click.
For debugging purposes, what happens if you put an else clause against the if(chk.checked)?
I think you're just missing this tag:
UseSubmitBehavior="false"
Try it like this:
<asp:Button ID="btnUpdate" runat="server" UseSubmitBehavior="false" OnClientClick="if(Page_ClientValidate()) { this.disabled = true; } else {return false;}" Text = "Update" CssClass="button" OnClick="btnUpdate_Click" ValidationGroup="vgNew"/>
Explanation