Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 316
Hitz Avatar asked May 08 '09 16:05

Hitz


People also ask

How do I disable a button after clicking?

For Disabling a button you can create an boolean attribute in the component and use this attribute in the disabled attribute of button tag. Whenever you want to disable the button in the click you can make the attribute to true,so that the button will be disabled.

How do I make a button disabled?

To disable a button using only JavaScript you need to set its disabled property to false . For example: element. disabled = true . And to enable a button we would do the opposite by setting the disabled JavaScript property to false .

How do you disable a button when another button is clicked HTML?

You can just add "disabled" to your button, and then when the user locks in their answer, enable it again. Additionally, it's not best practice to split your JavaScript into a bunch of different script tags. Put them all in one place.

Can I disable a button with CSS?

To make the disabled button, we will use the Pure CSS class “pure-button-disabled” with the class “pure-button”. We can also create a disabled button using disabled attribute. Disabled Button used Class: pure-button-disabled: It is used to disable the Pure CSS button.


2 Answers

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.

like image 143
Dave Ward Avatar answered Sep 22 '22 05:09

Dave Ward


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.

like image 36
Jimmie R. Houts Avatar answered Sep 22 '22 05:09

Jimmie R. Houts