Disable an ASP.NET button after clicking

Posted Friday, April 18, 2008 3:12 PM by Nathan Zaugg

I was creating a payment form recently and I wanted to disable the submit button after the user had clicked it so that there was no chance of them clicking it twice on accident. 

This seems like one of those things that just ought to be a slam dunk!  Back when I was doing ASP, this was one of the easiest things I ever did.  I have done it with asp.net before, but I never needed validation to work as well.  It took me a long time, many searches and trial and error but I found a solution that will disable the button and doesn't get in the way of validation.

protected override void OnInit(EventArgs e) { // We need this to get our button event Form.SubmitDisabledControls = true; btnProcessPayment.UseSubmitBehavior = false;   // Attach javascript code to the OnClientClick event btnProcessPayment.OnClientClick = "javascript :if ( Page_ClientValidate() ) " + "{this.disabled=true; this.value='Please Wait...'};";   base.OnInit(e); }

The javascript is pretty straight-forward.  We are simply calling the validation function ourselves, if it validated we disable the button and change the text.  What does the rest of the code do then? Well, normally when a control is disabled it doesn't post data on postback.  Fortunantly, ASP.NET makes it easy to override that default behavior.  Without this data our button click event would never fire!

You can also do this in the HTML as well.

 

<form id="form1" submitdisabledcontrols="true" runat="server"> <asp:button id="btnProcessPayment" onclick="btnProcessPayment_Click" runat="server" onclientclick="javascript :if ( Page_ClientValidate() ) {this.disabled=true; this.value='Please Wait...'};" usesubmitbehavior="false" text="Process Payment" />

 

I hope this is useful to someone!  If you end up using it on one of your sites, please leave a comment!

Filed under:

Comments

# re: Disable an ASP.NET button after clicking

Friday, March 20, 2009 10:29 AM by Shane

I've copied and pasted your example straight into my application and given it a whirl... the button does indeed disable and the text changed to "Please Wait...", but I get a small 'Error' in the status bar of IE7. I put a break point in the beginning of the  click event code of the button and it does not fire.

The button is not inside an AJAX update panel or anything like that.

Is there anything I should be looking out for to try get this working?

# re: Disable an ASP.NET button after clicking

Wednesday, April 01, 2009 10:49 AM by John Hennesey

Great stuff!  Thanks!

# re: Disable an ASP.NET button after clicking

Thursday, April 02, 2009 8:44 PM by Edward

I was searching high-and low for this solution.  Thanks alot!

# re: Disable an ASP.NET button after clicking

Tuesday, April 21, 2009 2:06 PM by Mike

It worked for me!!!!

# re: Disable an ASP.NET button after clicking

Tuesday, April 21, 2009 10:08 PM by Gabriel

Thank you so much. I spent at least an hour trying other samples that didn't work. Thanks!

# re: Disable an ASP.NET button after clicking

Monday, May 04, 2009 11:15 AM by Nathan Zaugg

If you don't have any validation controls on your form change the Javascript to:

"javascript : {this.disabled=true; this.value='Please Wait...'};"

# How To Disable A Button After Click In Asp Net | Spring Tipstoday

Pingback from  How To Disable A Button After Click In Asp Net | Spring Tipstoday

# How To Disable Validation Control In Asp.net On Button Click | Spring Tipstoday

Pingback from  How To Disable Validation Control In Asp.net On Button Click | Spring Tipstoday

# How To Disable Updatepanel On Button | Spring Tipstoday

Pingback from  How To Disable Updatepanel On Button | Spring Tipstoday