how do I disable Button asp.net

后端 未结 4 2154
旧时难觅i
旧时难觅i 2021-02-12 10:57

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条回答
  • 2021-02-12 11:12

    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;
    
    }
    
    0 讨论(0)
  • 2021-02-12 11:15

    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;
    }
    
    0 讨论(0)
  • 2021-02-12 11:20

    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 
    
    0 讨论(0)
  • 2021-02-12 11:26

    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;
    
    0 讨论(0)
提交回复
热议问题