Angus Logan

MCMS/SPS/.NET/SQL/Microsoft Australia

Disable Button On PostBack (ASP.NET) - also works with validation controls

From  Mark Daunt

How many times have you seen the problem where somebody clicks on the Submit button a hundred times?  One technique to avoid multiple submissions is to use client side script to disable the button after it has been clicked.  Problem is you only want to disable the button if the form is valid and is actually going to PostBack.  To get around this call the client side function Page_ClientValidate() to see if the page is valid.  If it is you can set a caption on the button, like “Please Wait...“, disable it, and invoke its click event,  like in the following code sample:

System.Text.StringBuilder sbValid = new System.Text.StringBuilder();

sbValid.Append("if (typeof(Page_ClientValidate) == 'function') { ");

sbValid.Append("if (Page_ClientValidate() == false) { return false; }} ");

sbValid.Append("this.value = 'Please wait...';");

sbValid.Append("this.disabled = true;");

sbValid.Append("document.all.btnSubmit.disabled = true;");

//GetPostBackEventReference obtains a reference to a client-side script function that causes the server to post back to the page.

sbValid.Append(this.Page.GetPostBackEventReference(this.btnSubmit));

sbValid.Append(";");

this.btnSubmit.Attributes.Add("onclick", sbValid.ToString());

 

Posted: Dec 22 2004, 10:13 PM by anguslogan | with 20 comment(s)
Filed under:

Comments

anguslogan said:

re: Disable Button On PostBack
# February 19, 2005 7:51 PM

anguslogan said:

I get a "Object not set to an instance of an object" at line "sbValid.Append("if (typeof(Page_ClientValidate) == 'function') { ");".

Any idea why?
# March 1, 2005 1:56 AM

anguslogan said:

This worked except the line for the line of "sbValid.Append("document.all.btnSubmit.disabled = true;"); "

This was causing an error on the page so I committed it out. I also added a disable button to the post back to keep the button disable (that is what I needed) and this solution worked great. Thanks for the help.
# March 9, 2005 3:47 AM

anguslogan said:

Excellent Piece of code, exactly what I was looking for, thanks for posting it.
# March 15, 2005 9:47 PM

anguslogan said:

This code worked first time, you need to replace the btnSubmit with the ID of your button, then everything is fine
# March 15, 2005 9:49 PM

anguslogan said:

I get a "Object not set to an instance of an object" at line "sbValid.Append("if (typeof(Page_ClientValidate) == 'function') { ");".

Any idea why?

Place it in a Page_Load

Like
void Page_Load(Object Sender, EventArgs e)
{
System.Text.StringBuilder sbValid = new System.Text.StringBuilder();

sbValid.Append("if (typeof(Page_ClientValidate) == 'function') { ");

sbValid.Append("if (Page_ClientValidate() == false) { return false; }} ");

sbValid.Append("this.value = 'Please wait...';");

sbValid.Append("this.disabled = true;");

sbValid.Append("document.all.btnSubmit.disabled = true;");

//GetPostBackEventReference obtains a reference to a client-side script function that causes the server to post back to the page.

sbValid.Append(this.Page.GetPostBackEventReference(this.btnSubmit));

sbValid.Append(";");

this.btnSubmit.Attributes.Add("onclick", sbValid.ToString());

}
<form runat="server">
<asp:Button id="btnSubmit" text="whatever" runat="server" />
</form>
# March 18, 2005 10:03 AM

anguslogan said:

Excellent...works well
# April 28, 2005 11:58 AM

anguslogan said:

This code doesn`t work using VB in ASP.Net. The reference to this produces an error: this is not declared.
# May 13, 2005 7:47 AM

anguslogan said:

It will work in Vb.Net. Simple make the following changes:

sbValid.Append(Me.Page.GetPostBackEventReference(me.cmdMakeNonSupervisor ))

-mstrclark
# June 22, 2005 12:39 AM

anguslogan said:

What about client side validation? I want to preform Client-side validation and, if successfull, disable the button and postback. If client-side validation fails, then leave the button enabled.

Any ideas or did I miss something in the code above?

Thanks,

MstrClark
# June 22, 2005 12:41 AM

anguslogan said:

Worked Great on a standard aspx form. Thank you very much. However, it did not work in a custom control (ascx). It worked, except it did not cause a post back. Any ideas?
# July 8, 2005 8:30 AM

anguslogan said:

This is great!! Question: how do you disable all buttons within a page onclick instead of just the current button?
# October 19, 2005 7:57 AM

anguslogan said:

Just what I was looking for after stuffing around with client-side options, worked like a charm first time. Cheers!
# November 12, 2005 12:18 PM

anguslogan said:

a
# November 29, 2005 6:55 PM

bill said:

doesn't work with firefox, at least using ASP.net master pages

# September 18, 2007 7:24 AM

Gump said:

Thanks, this is still valuable info even a couple years later!

# September 21, 2007 1:55 PM

bn bnbvn said:

Voce ta louco seu porra ?!

# October 11, 2007 4:53 PM

notanguslogan said:

BEAUTIFUL

thank you, lots of ppl off my back because of this

# November 7, 2007 10:22 AM

Jportelas said:

Hi there, I came up with this same solution a time ago, it works with .net 1.0 and 1.1, don´t you have something like it with asp.net 2.0?

# December 13, 2007 11:01 AM

amit raut said:

If you have validation group say 'Send' on the button, write

sbValid.Append("if (Page_ClientValidate('Send') == false) { return false; }} ");

you have to specify validation group name for function Page_ClientValidate.

# June 26, 2009 2:17 AM