Disable Button On PostBack (ASP.NET)

One of the problems with web forms is when user clicks on Submit button several times, it will do multiple submissions. To prevent this we can easily use client side script to disable the Submit button. But then next problem you face after disabling the Submit button is to do the PostBack (after disabling the Submit button it stops the page PostBack happens).

I came across an article which shows how you can get around this problem by calling a client side function. Hope this would help you guys if facing the same problem like I did.

client side function (Page_DoPostBack) as follows

System.Text.StringBuilder sbValid = new System.Text.StringBuilder();
sbValid.Append("if (typeof(Page_DoPostBack) = = 'function') { ");
sbValid.Append("if (Page_DoPostBack () == false) { return false; }} ");
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());

1 comments