How to disable an ASP.NET button when clicked
Scenario: user clicks a button in your ASP.NET page and you want to disable it immediately using javascript so that the user cannot accidentally click it again.
Originally I thought this was going to be simple by using the onclientclick property of the ASP.NET button server control but although that worked to disable the button, it did not continue with the form postback. Eventually after quite a bit of Googling and some more failed attempts I figured out this solution:
MyButton.Attributes.Add("onclick", "this.disabled=true;" + Page.ClientScript.GetPostBackEventReference(MyButton, "").ToString());
Adding the javascript to disable the button to the onclick event by appending to the Attributes collection of the ASP.NET button control but then also calling the Post Back Event Reference for the button right after disabling the button. That is why my original try at adding the javascript failed, adding the this.disabled=true to the onclientclick event then overwrote the call to the post back event.
With the code above now the button onclick event will look something like this:
onclick="this.disabled=true;__doPostBack('MyContrl$MyButton','');"
The button is disabled AND then the button post back method is called.