Site Search:
Sign in | Join | Help
4Penny.net

ASP.NET

Notes, Tricks and Tips on ASP.NET Coding

Disabling buttons on click in ASP.NET

Just to give props, I found this here: http://geekswithblogs.net/jwhitehorn/archive/2006/06/17/82214.aspx

but I don't like to leave valuable info on the internet, it might get deleted. Plus, I have to spend the 30 minutes hunting it down again <smiles>

We have all seen the websites that disable "submit" buttons when you click on them. This is often done to prevent users from clicking the button multiple times.
Normally this is accomplished using an 'onclick' JavaScript event to disable the button. In ASP.NET, each server side item already has a onclick event handler which calls the server back for event processing.
To accomplish the same thing in ASP.NET, you could easily do:

 Button1.Attributes.Add("onclick", "this.disabled=true;" + ClientScript.GetPostBackEventReference(Button1, "").ToString()) 

Comments

No Comments

About Steve Gray

Steve is a seasoned (translate: old) developer in VB and ASP.NET. He spends most of his time in Dynamics GP, writing custom mods for consulting firms. Crystal reports, eConnect, VS Tools for Dynamics... anything that comes along.