23

How can I disable or enable button in asp.net? I want to disable button after click to prevent double click. I am trying to disable my Login button after clicking on it.

4
  • I am not able to disable button. when user enter username and password and click on login button at that time button is not disable and also want to enable it when user failed to login.
    – Amol
    Nov 24, 2012 at 5:36
  • 1
    take a look to this jsfiddle
    – BrOSs
    Nov 24, 2012 at 5:43
  • It will post the values to the server and then you want to disable this button. Right? Nov 24, 2012 at 6:16
  • @krshekhar Yes, it requires
    – Amol
    Nov 24, 2012 at 6:24

4 Answers 4

24

You have to disable it on client so that user could not click it again.

<asp:button id="btn" runat="server" OnClientClick="this.disabled=true;"......

To disable on server side asp.net code.

btn.Enabled = false;
6
  • if user click with out entering username and password it goes to disable.
    – Amol
    Nov 24, 2012 at 6:04
  • 1
    @adil even if he writes the user name and password and then click and if you disable the button it will not post the values to the server. Nov 24, 2012 at 6:18
  • 1
    Could it be possible to put a new question for that as that is entirely different from OP
    – Adil
    Nov 24, 2012 at 6:20
  • 2
    @Șhȇkhaṝ I noticed the same (the button would disable, but the code behind would not fire) -- I added UseSubmitBehavior=False and it is working as desired.
    – bdimag
    Jun 30, 2014 at 18:03
  • how to disable without click??
    – Elyor
    Sep 9, 2015 at 9:46
4

You can use the client-side onclick event to do that:

yourButton.Attributes.Add("onclick", "this.disabled=true;");

or

You can do this with javascript. in your form tag,

onsubmit="javascript:this.getElementById("submitButton").disabled='true';"

or

In code behind file you can do like this

button1.enabled = false 
2
  • if user click with out entering username and password it goes to disable.
    – Amol
    Nov 24, 2012 at 6:03
  • 1
    @Amol you should specify that in your question. Oct 14, 2013 at 14:56
2

write a java-script function which checks the user name and password.
If they are not blank the disable the button.
But if you disable the button and there is a postback. And after the postback still it will be enable.
So Idea is

  1. Create a java-script function.
  2. validate user-name and password
  3. if they are valid
  4. disable the button (javascript).
  5. Add ClientIdMode="Static" to your <asp:button> to prevent .NET from mangling the name.

--edit

<asp:button id="btn" runat="server" ClientIdMode="Static" OnClientClick="return btn_disable" ...

Your java-script code

function btn_disable
{
   //check for user name and password
   // if filled
   document.getElementById("btn").disabled=true;

}
2
  • 1
    nice try but how about the bs webforms does with your id naming?
    – Viking
    Sep 22, 2017 at 10:42
  • 1
    @Developerdeveloperdeveloper use ClientIdMode="Static"
    – nu everest
    Mar 1, 2018 at 14:46
0

Front-end only:

<button id="loginbtnID" onclick="DisableBtn()">Log in</button>

<script type="text/javascript">
  function DisableBtn() {
    document.getElementById("loginbtnID").disabled = true;
  }
</script>

Front-end & Code Behind:

(this reloads the whole page though.. check this to prevent reloading)

disablebutton.aspx:

<button id="loginbtnID" runat="server" onclick="DisableBtn()" OnClick="codeBehindFunction">Log in</button>

disablebutton.aspx.cs:

protected void codeBehindFunction(object sender, EventArgs e)
{
  loginbtnID.Disabled = false;
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.