Disabling Controls During AJAX Calls

 

When there is an AJAX call going through, you should disable the control that makes the AJAX call and also the controls whose contents will be updated by the AJAX call. In this tutorial, I will show how to disable the controls during AJAX calls.

Creating the Mock-up AJAX feature

Consider a situation when a user logs in to a website. He/she enters the username and password and then clicks the Submit button. The HTML code of the control is given below.

  1. <div id="content">  
  2.     <h3><b><u>Login</u></b></h3>  
  3.     <table>  
  4.         <tr>  
  5.             <td>Username:  
  6.             </td>  
  7.             <td>  
  8.                 <input type="text" id="userNameTextBox" />  
  9.             </td>  
  10.         </tr>  
  11.         <tr>  
  12.             <td>Password:  
  13.             </td>  
  14.             <td>  
  15.                 <input type="text" id="passwordTextBox" />  
  16.             </td>  
  17.         </tr>  
  18.         <tr>  
  19.             <td colspan="3">  
  20.                 <input type="submit" id="submitButton" value="Login" />  
  21.             </td>  
  22.         </tr>  
  23.     </table>  
  24.     <img id="loadingImg" src="loading.gif" />  
  25.     <div id="messageDiv"></div>  
  26. </div>  

I have a “content” div which contains a “table”. The “table” contains “Username” & “Password” input controls along with a “submitButton”.

After the table, there is an “img” control that will show the loading image during AJAX call. I have also kept a “messageDiv” that will show the message when a user gets logged in to the website.

Note

My aim here is to disable the “username” & “password” input controls and also, the button control when AJAX call is made. I will also show the loading image over the center of the “Content” div during AJAX calls.

Add the CSS to the page

The CSS which you have to add to this page is mentioned below.

  1. #content {  
  2.             positionrelative;  
  3.             borderdashed 2px #CCC;  
  4. }  
  5.   
  6.     #conten#loadingImg {  
  7.         displaynone;  
  8.         positionabsolute;  
  9.         marginauto;  
  10.         top: 0;  
  11.         left: 0;  
  12.         right: 0;  
  13.         bottom: 0;  
  14.     }  
  15.   
  16.     #content table {  
  17.         marginauto;  
  18.     }  
  19.   
  20.     #conten#messageDiv {  
  21.         text-aligncenter;  
  22.     }  

Note

To show the image at the center of “content” div, first, set the position of the “content” div to "relative". Then, for the image, set its position to "absolute", margin to "auto", and “top,left,right & bottom” properties to 0.

Initially, the image will remain hidden but when AJAX call starts, it will be shown. Similarly, on completion of the AJAX call, the image will again be hidden.

Add the jQuery code to the page 

On the button click, the AJAX call will be made using .ajax() method. The jQuery code for this is as follows.

  1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>  
  2. <script type="text/javascript">  
  3.     $(document).ready(function () {  
  4.         $(document).ajaxStart(function () {  
  5.             $("table").css("background-color""Grey");  
  6.             $("table input").prop("disabled", true);  
  7.   
  8.             $("#loadingImg").show();  
  9.         });  
  10.   
  11.         $(document).ajaxStop(function () {  
  12.             $("#loadingImg").hide();  
  13.             $("table").hide();  
  14.         });  
  15.   
  16.         $("#submitButton").click(function (e) {  
  17.             if (($("#userNameTextBox").val() == "") || ($("#passwordTextBox").val() == ""))  
  18.                 alert("Your must fill both Username and Password");  
  19.             else {  
  20.                 $.ajax({  
  21.                     type: "POST",  
  22.                     url: "index.aspx/login",  
  23.                     contentType: "application/json; charset=utf-8",  
  24.                     data: '{"username":"' + $("#userNameTextBox").val() + '","password":"' + $("#passwordTextBox").val() + '"}',  
  25.                     dataType: "json",  
  26.                     success: function (msg) {  
  27.                         if (msg.d) {  
  28.                             $("#messageDiv").html(msg.d);  
  29.                         }  
  30.                     },  
  31.                     error: function (req, status, error) {  
  32.                         alert(req + " " + status + " " + error);  
  33.                     }  
  34.                 });  
  35.             }  
  36.             return false;  
  37.         });  
  38.     });  
  39. </script>  

The .ajaxStart() method will be called when the AJAX call starts while .ajaxStop() method is called when the AJAX call has returned some value.

Inside in the .ajaxStart() method, I am setting the background of the table to grey color and disabling all the input controls (2 textboxes and 1 button). I am also showing the image control which will show in the middle of the “content” div (check its CSS).

Note - You can also do the AJAX call using jQuery Post method. The .ajaxStart() & .ajaxStop() methods will work the same way. 

Similarly, in the .ajaxStop() method, I am hiding both the table and the image. If you check the jQuery AJAX code under the button click event, you will see that I have called the C# function named “login” and passing to it the 2 textboxes values.

This C# login function will get the username and password values, then it checks them against the database stored login and passwords. If it finds them on the database table, the user is logged in.

The code of the login function is as following.
  1. [System.Web.Services.WebMethod]  
  2. public static string login(string username, string password)  
  3. {  
  4.     //checks the database values and returns the appropriate message.  
  5.     System.Threading.Thread.Sleep(2000);  
  6.     return "Login Successful";  
  7. }  
Note - To see the loading image, I have delayed the AJAX response by 2000 milliseconds.
 
Testing the page

Run the page in the browser, enter any value for username and password, and click the Login button. The loading image will show in the middle of the “content” div and the input controls will be disabled.

 

After 2 seconds (approximately), when AJAX call completes, both, the image and the table, are hidden.

You can see the demo of this feature here - DEMO

Up Next
    Ebook Download
    View all
    Learn
    View all