Nates Stuff

Perspectives on Computing :: Leave a Comment

Disable an ASP.NET button after clicking

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!

Published Friday, April 18, 2008 3:12 PM by Nathan Zaugg
Filed under:

Comments

# re: Disable an ASP.NET button after clicking@ Friday, March 20, 2009 10:29 AM

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?

by Shane

# re: Disable an ASP.NET button after clicking@ Wednesday, April 01, 2009 10:49 AM

Great stuff!  Thanks!

by John Hennesey

# re: Disable an ASP.NET button after clicking@ Thursday, April 02, 2009 8:44 PM

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

by Edward

# re: Disable an ASP.NET button after clicking@ Tuesday, April 21, 2009 2:06 PM

It worked for me!!!!

by Mike

# re: Disable an ASP.NET button after clicking@ Tuesday, April 21, 2009 10:08 PM

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

by Gabriel

# re: Disable an ASP.NET button after clicking@ Monday, May 04, 2009 11:15 AM

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

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