Disabling button when performing some operation in ASP.NET

In ASP.NET Web Application when we submit some button to process information in server side, we can disable the button as long the server performing the operation and enable the same one response is back. Most of the developer use Button Enable and Disable properties to enable and disable the button. But as an good practice we can use Page.ClientScript.GetPostBackEventReference to achieve the same.

Button1.OnClientClick = ClientScript.GetPostBackEventReference(Button1, "") + "; this.value='Processing...';this.disabled = true;";

To do a quick test, you can write the below code block

protected void Page_Load(object sender, EventArgs e)
{
Button1.OnClientClick = ClientScript.GetPostBackEventReference(Button1, "") + "; this.value='Processing...';this.disabled = true;";
}
protected void Button1_Click(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(10000);
}
}

image

DON'T MISS OUT!
Subscribe To Newsletter For your Daily Development Tips and Tricks  

 Best Tips & Tricks on Microsoft Technology Platform 

Invalid email address
We promise not to spam you. You can unsubscribe at any time.
Abhijit Jana

Abhijit runs the Daily .NET Tips. He started this site with a vision to have a single knowledge base of .NET tips and tricks and share post that can quickly help any developers . He is a Former Microsoft ASP.NET MVP, CodeProject MVP, Mentor, Speaker, Author, Technology Evangelist and presently working as a .NET Consultant. He blogs at http://abhijitjana.net , you can follow him @AbhijitJana . He is the author of book Kinect for Windows SDK Programming Guide.

One Comment to “Disabling button when performing some operation in ASP.NET”

Comments are closed.