How to Disable Buttons during AJAX Postback in ASP.Net

How to easily disable buttons during AJAX postbacks or other operations so that you don't end up doing multiple requests.

By Tim TrottC# ASP.Net MVC • February 5, 2010
How to Disable Buttons during AJAX Postback in ASP.Net

It is an often overlooked requirement to disable controls on postback during an AJAX postback event to prevent repetitive clicking, a minor problem which can become more serious depending on the action being taken. Payment processing, for example, can result in multiple payments or orders if not handled correctly.

I'll start with a very basic AJAX form - a single button and an UpdateProgress control and from here we'll look at how to disable buttons during AJAX Postback. This also works for any other control that you wish to update during a postback request.

xml
<form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <div>
        
        <asp:UpdatePanel ID="updPanel1" runat="server">
            <ContentTemplate>
                <asp:Button runat="server" Text="Submit" ID="btnSubmit" OnClick="btnSubmit_Click"/>
                <asp:UpdateProgress runat="server" ID="ajaxProgress">
                <ProgressTemplate>Please wait...</ProgressTemplate>
                </asp:UpdateProgress>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
</form>

I have bound the OnClick event to the btnSubmit_Click which will create a delay of 5 seconds.

C#
protected void btnSubmit_Click(object sender, EventArgs e)
{
    System.Threading.Thread.Sleep(5000);
}

If you create a new AJAX-enabled website, copy the above code and run the site you will notice that when you click the button the progress is shown and you can click the button as many times as you want. Depending on your situation this can cause many undesirable effects such as multiple record creation, duplicate payment processing and higher server loads.

This can all be combated using the following Javascript. The only thing you need to do is change the name of the button to whatever you are calling it. You can also add the name of any other control on the form - text boxes, drop-down lists and so on. These will also be disabled while the postback is in progress. Remember to modify both the instances where btnSubmit exists otherwise, you may find that your controls do not get enabled again.

javascript
(function() {  
  Sys.Application.add_load(setupButtonDisablers);  
  
  function setupButtonDisablers() {  
     Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(onPageLoad);  
     Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(onSubmit);  
     Sys.Application.remove_load(setupButtonDisablers);  
  }  
  
  function onPageLoad() {  
     findAndEnable('<%= btnSubmit.ClientID %>'); // Change this
  }  
  
  function onSubmit() {  
     findAndDisable('<%= btnSubmit.ClientID %>'); // Change this
  }  
  
  function findAndDisable(id) {  
     findAndSetDisabledProperty(id, true);  
  }  
  
  function findAndEnable(id) {  
     findAndSetDisabledProperty(id, false);  
  }  
  
  function findAndSetDisabledProperty(id, value) {  
     var control = $get(id);  
     if (control) {  
         control.disabled = value;  
     }  
  }  
})();

About the Author

Tim Trott is a senior software engineer with over 20 years of experience in designing, building, and maintaining software systems across a range of industries. Passionate about clean code, scalable architecture, and continuous learning, he specialises in creating robust solutions that solve real-world problems. He is currently based in Edinburgh, where he develops innovative software and collaborates with teams around the globe.

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

My website and its content are free to use without the clutter of adverts, popups, marketing messages or anything else like that. If you enjoyed reading this article, or it helped you in some way, all I ask in return is you leave a comment below or share this page with your friends. Thank you.

There are no comments yet. Why not get the discussion started?

New comments for this post are currently closed.