Monday, August 24, 2009

Disable a Button (Client Side)

This code is great for when you want to make sure a person doesn't click a button more than once - but do it in a prettier way (you of course should verify everything in your code-behind as well, but this makes it pretty damn clear to your users):

StringBuilder sbValid = new 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.forms[0].ctl00_ContentPlaceHolder1_btnPaymentSubmit.disabled = true;");
sbValid.Append(this.Page.GetPostBackEventReference(this.btnPaymentSubmit));
sbValid.Append(";");
btnPaymentSubmit.Attributes.Add("onclick", sbValid.ToString());

No comments:

Post a Comment