14

I have a button control. Once the user clicks on it, the click event should fire and then the button should get disabled. How can I do this? I have the option to use JQuery or JavaScript or both.

Here is my button declaration:

<asp:Button 
  ID="Button1" 
  runat="server" 
  Text="Click Me" 
  onclick="Button1_Click"
/> 

On the button click code behind, I have added a Response.Write(). That should get executed and then the button should be disabled

Chris Farmer
  • 24,974
  • 34
  • 121
  • 164
Hitz
  • 1,041
  • 4
  • 12
  • 27

9 Answers9

32

For whatever reason, the HTML spec dictates that disabled elements should not be included in POST requests. So, if you use JavaScript to disable the HTML element in the client-side onclick event, the input element will be disabled when the browser assembles the POST request, the server won't be properly notified which element raised the postback, and it won't fire server-side click event handlers.

When you set the UseSubmitBehavior property to false, ASP.NET renders an input element of type button instead of the regular input of type submit that the ASP.NET Button control normally generates. This is important because clicking a button element does not trigger the browser's form submit event.

Instead of relying on a browser form submission, ASP.NET will render a client-side call to __doPostBack() within that button element's onclick handler. __doPostBack will raise the postback explicitly, regardless of what POST data comes through in the request.

With the postback being raised independent of the browser submit event, you're freed of the previously mentioned HTML quirk. Then, you can set an OnClientClick of "this.disabled = true;", which will render as "this.disabled = true; __doPostBack('Button1', '');", and things will work as intended.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Dave Ward
  • 59,815
  • 13
  • 117
  • 134
9

add an OnClientClick="this.disabled = true;" to your button.

If you are using Asp.net Ajax you might want to look at using PostBack Ritalin.

Jimmie R. Houts
  • 7,728
  • 2
  • 31
  • 38
  • 5
    You also need UseSubmitBehavior="false". See http://stackoverflow.com/questions/400616/why-doesnt-my-form-post-when-i-disable-the-submit-button-to-prevent-double-click – Pavel Chuchuva Jun 27 '09 at 08:36
  • The page will post back but will the button click event fire if you set it's disable property to true? I am sure it will not. I am right. The answer by Dave Ward below explains the same – Ankur-m Jun 19 '12 at 05:41
2

Have you tried this?

Add an OnClientClick="MyFunction();" to your .NET button.

Then in the .aspx page script tags you add the following JS function:

<script type="text/javascript">
  function MyFunction()
  {
    window.setTimeout(function ()
    {
      // get the button/control to disable using your favourite clientside ...
      // ... control grabbing code snippet ...
      // ... eg. JQUERY $('Button1'), getElementById, etc.)

      document.getElementsByName('Button1').Button1.disabled = true;

      // I've used "getElementsByName" because .NET will render a button with
      // ... a "name" attribute, and not an "id" attribute, by default

    }, 1);
  }
</script>

This gives the browser a chance to post back, followed by a quick button disable.

Funzi
  • 21
  • 4
0

When using the "this.disabled = true" method make sure you check if the page is valid before disabling the control if you have validators on the page. If validation fails you won't be able to re-enable the control without reloading the page.

if (Page_IsValid) this.disabled = true;
Jonathan Foster
  • 163
  • 2
  • 7
0
   <script type="text/javascript">
     Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
      function BeginRequestHandler(sender, args) {
        document.getElementById('<%= lblMessage.ClientID %>').innerText = "Processing...";
          document.getElementById('<%= btnSubmit.ClientID %>').innerText = "Processing";
         args.get_postBackElement().disabled = true;
           }
           </script>

Add Script Tag in source page . change Id of button in code . You can disable the button till the process completes execution .

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
pavan
  • 16
0

You need to be careful that the postback occurs before you disable the button through client script. This is a common gotcha with ajax and input boxes. Disabling an input box prevents the data from being sent from the browser, even if you can see text within it while it is disabled. The answer is that you need to use jquery for this to ensure the server-side code runs first before it is disabled.

-Oisin

x0n
  • 51,312
  • 7
  • 89
  • 111
  • I did it..I ran this code but the code behind never executed $('input[type=submit]').click(function() { this.disabled = true; }); – Hitz May 08 '09 at 16:53
0
// to disable
this.setAttribute('disabled', true); 
// to enable
this.removeAttribute('disabled');

this is a cross browser solution

Chad Cache
  • 9,668
  • 3
  • 56
  • 48
0

There is really cool event for body tag "<"body onBeforeunload="buttonId.disabled = true;" ">"

This event triggers right before form submits, in other words your data will be submitted correctly.

pyccki
  • 693
  • 8
  • 22
-3

you can disable it server side

Button1.Enabled = false;
Paul U
  • 671
  • 6
  • 7
  • @op: "On the button click code behind, I have added a Response.Write(). That should get executed and then the button should be disabled" The onClientCLick would disable the button before the server response. In any case, I would appreciate a reason for the down vote. – Paul U Jun 12 '09 at 21:41