I have an ASP.NET button that I need to disable after the user clicks it to prevent double-clicking. Once the submit completes it has to be enabled again. Can anyone help me with this?
On Click of ASP.Net Submit button, we disable the button using javascript so that user cannot submit it again. But disabling the button will prevent the form submission, as you cannot submit the form if the button is disabled. So we call the ASP.Net PostBack event method directly.
submit(function () { $('input[type=submit][data-loading]'). addClass('disabled'); if ($(this). data('submitted') == true) { $('input[type=submit][data-loading]'). attr('disabled', 'disabled'); return false; } $(this).
I have the following code for avoiding the double clic: jQuery. fn. preventDoubleSubmit = function () { var alreadySubmitted = false; return jQuery(this).
The postback on submit button can be avoided by giving return=false in the event handler function as below.
Here is a solution that works for the asp.net button object. On the front end, add these attributes to your asp:Button definition:
<asp:Button ... OnClientClick="this.disabled=true;" UseSubmitBehavior="false" />
In the back end, in the click event handler method call, add this code to the end (preferably in a finally block)
myButton.Enabled = true;
I have found this, and it works:
btnSave.Attributes.Add(
"onclick",
"this.disabled = true;" + ClientScript.GetPostBackEventReference(btnSave, null) + ";");
Check this link, the most simplest way and it does not disable the validations.
http://aspsnippets.com/Articles/Disable-Button-before-Page-PostBack-in-ASP.Net.aspx
If you have e.g.
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button1" OnClick="Button1_Clicked" />
</form>
Then you can use
<script type = "text/javascript">
function DisableButton() {
document.getElementById("<%=Button1.ClientID %>").disabled = true;
}
window.onbeforeunload = DisableButton;
</script>
To disable the button if and after validation has succeeded, just as the page is doing a server postback.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With