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.

Technorati Tags: ,


11 Comments

  • You can avoid the GetPostBackEventReference, by setting the Button's UseSubmitBehavior to false. That will automatically inject an appropriate __doPostBack().

    See this for more info: http://encosia.com/2007/04/17/disable-a-button-control-during-postback/

  • Hi,

    Why not use the onclientclick to add the javascript
    MyButton.OnClientClick = "this.disabled=true;";

  • Hi Vikram,
    That is what I initially tried but then the button did not postback because the postback event is overwritten. So you need to add the postbackevent to the onclick like I did in the code behind or use gt1329a's solution which is to set the UseSubmitBehavior property to false.
    -Jeff

  • Thank you so much. I think this fix can also be used when showing/hiding div statements in javascript when a button is clicked.

  • It is useful. But i am not interest in Java script. I am always confuse with Java script. but anyways thanks for sharing information.

  • tarjeta, you should be interested in Javascript if you are a web developer, regardless if it confuses you or not.

    Unconfuse yourself and do some studying.

    Just sayin'.

    Thanks for the post, Jeff

  • this disables my requiredfieldvalidators

  • I've been troubled for several days with this topic. <a href="https://cse.google.sc/url?sa=t&url=https%3A%2F%2Fwww.mtclean.blog/">baccaratcommunity</a> Your posts are neatly organized with the information I want, so there are plenty of resources to reference. I bookmark this site and will find your posts frequently in the future. Thanks again ^^

  • I am very impressed with your writing <a href="https://cse.google.ru/url?sa=t&url=https%3A%2F%2Fwww.mtclean.blog/">majorsite</a> and I'm really happy to find a post with the material I was looking for! I bookmark and will come often! Thanks :D

  • I came to this site with the introduction of a friend around me and I was very impressed when I found your writing. I'll come back often after bookmarking! <a href="https://cse.google.pt/url?sa=t&url=https%3A%2F%2Fwww.mtclean.blog/">casino online</a> I couldn't think of this, but it's amazing! I wrote several posts similar to this one, but please come and see!

  • It's the same topic , but I was quite surprised to see the opinions I didn't think of. My blog also has articles on these topics, so I look forward to your visit. <a href="https://cse.google.pn/url?sa=t&url=https%3A%2F%2Fwww.mtclean.blog/">totosite</a> I would like to write an article based on your article. When can I ask for a review?

Add a Comment

As it will appear on the website

Not displayed

Your website