Hitz
Hitz

Reputation: 1071

Disable a button on click

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

Upvotes: 14

Views: 22504

Answers (9)

Dave Ward
Dave Ward

Reputation: 60590

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.

Upvotes: 32

pavan
pavan

Reputation: 16

   <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 .

Upvotes: 0

Funzi
Funzi

Reputation: 21

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.

Upvotes: 2

Jonathan Foster
Jonathan Foster

Reputation: 343

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;

Upvotes: 0

pyccki
pyccki

Reputation: 693

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.

Upvotes: 0

Chad
Chad

Reputation: 9859

// to disable
this.setAttribute('disabled', true); 
// to enable
this.removeAttribute('disabled');

this is a cross browser solution

Upvotes: 0

Paul U
Paul U

Reputation: 681

you can disable it server side

Button1.Enabled = false;

Upvotes: -3

x0n
x0n

Reputation: 52480

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

Upvotes: 0

Jimmie R. Houts
Jimmie R. Houts

Reputation: 7828

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.

Upvotes: 9

Related Questions