Tuesday, January 30, 2007

Click Once Button

It is not very uncommon to want us to stop double clicking of submit buttons in our web applications. For example, the purchase button on your web page should be clicked only once. Here is a method on how I go doing that and I call it Click-Once-Button.

Remember, always have server side validations for double or duplicate processing. This technique is a client-side script that presents a user friendly way of disabling the button once clicked.

// Click Once on Purchase:

StringBuilder sbJS = new StringBuilder();

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

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

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

sbJS.Append("this.src = '/Images/purchase_grey.gif';");

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

sbJS.Append("this.alt = 'Please wait...';");

sbJS.Append(Page.ClientScript.GetPostBackEventReference(imgbtnPurch, ""));

sbJS.Append(";");

imgbtnPurch.Attributes.Add("onclick", sbJS.ToString());



Love coding!

9 comments:

Anonymous said...

Briliiant! iv tried lots of code but this one works fine on ajax platform.

Anonymous said...

I have a problem with this code, it doesn't take effect the first time the button is clicked. It only seems to work after the page has been posted back. Any advice?

Kishore Gorjala said...

Where are you adding this code snippet? For example, did you try using it in Page_Load event? I'm guessing you are adding this on a postback event rather than initial load itself.

Hope this helps,

Madhanlal JM said...

Excellent Solution.. Helps me very much. Thank you.

Anonymous said...

Awesome easy solution for a once click button, thank much!

Anonymous said...

Thank you so much. This is simple, elegant, and AWESOME code!!

Anonymous said...

Thank u very much ! it help's me lot. simply AWESOME......

Anonymous said...

You can also do it without the c# code. See here:

http://lawo.wordpress.com/2010/05/14/disable-button-after-click-with-confirmation-clientside/

Anonymous said...

Excellent! I have tried several approaches one can find, but this one is one actually works with no conflict with jquery and AJAX
Thank You!