How to disable a button after 1st click How to disable a button after 1st click asp.net asp.net

How to disable a button after 1st click


Use the OnClientClick and UseSubmitBehavior properties of the button control.

<asp:Button runat="server" ID="BtnSubmit"   OnClientClick="this.disabled = true; this.value = 'Submit in progress...';"   UseSubmitBehavior="false"   OnClick="BtnSubmit_Click"   Text="Click to Submit" />

OnClientClick allows you to add client side OnClick script. In this case, the JavaScript will disable the button element and change its text value to a progress message. When the postback completes, the newly rendered page will revert the button back its initial state without any additional work.

The one pitfall that comes with disabling a submit button on the client side is that it will cancel the browser’s submit, and thus the postback. Setting the UseSubmitBehavior property to false tells .NET to inject the necessary client script to fire the postback anyway, instead of relying on the browser’s form submission behavior. In this case, the code it injects would be:

__doPostBack('BtnSubmit','')

Redered HTML:

<input type="button" name="BtnSubmit"   onclick="this.disabled = true; this.value = 'Submitting...';__doPostBack('BtnSubmit','')"  value="Submit Me!" id="BtnSubmit" />

This should give you the desired behavior.

From: http://encosia.com/disable-a-button-control-during-postback/Credit: Dave Ward (Twitter: @Encosia)


Have you tried?:

protected void Button3_Click(object sender, EventArgs e){   Button3.Enabled = false;   //rest of code}


<asp:Button  onclick="Button3_Click" ID="Button3" runat="server" Text="Save"OnClientClick="this.disabled = true; this.value = 'please wait ..';" UseSubmitBehavior="false"     />