JavaScript to enable/disable asp.net controls on textbox value change or typing text in TextBox

Introduction: In this article i am going to explain How to enable or disable asp.net controls on textbox value change or entering/typing text in TextBox or in other words we can say enable and disable  any asp.net controls like TextBox, Label, DropDownList, RadioButton, RadioButtonList, CheckBox, CheckBoxList etc as per requirement as soon as we start typing in the TextBox control in asp.net using Javascript. 

Enable and disable asp.net controls on starting typing in TextBox using JavaScript

Description: While working on asp.net project it was required to enable the reset button that clears the textbox when clicked. But on page load by default this button should be disabled and whenever we enter any character i.e. we start typing in the TextBox then the reset button should be enabled so that if required we can clear out the values we entered.
For this purpose i used the onkeyup event of JavaScript to check if anything has been entered in textbox or not? If yes then enable the reset button otherwise disable the button.


Implementation: Let's create a simple web page to demonstrate it by an example. 
  • In the <Head> tag of the design page (e.g. default.aspx) create a JavaScript function as:

<script type="text/javascript" language="javascript">
        function EnableDisableButton(sender, target) {
            if (sender.value.length > 0)
                document.getElementById('<%= btnReset.ClientID %>').disabled = false;
                else
                    document.getElementById('<%= btnReset.ClientID %>').disabled = true;
            }       
    </script>

Asp.Net C# Section
  •  In the <Form> tag design the asp.net page e.g. default.aspx as:

 <div>
    <fieldset style="width:320px">
    <legend>Enable/Disable asp.net control on typing in textbox</legend>
    <asp:TextBox ID="txtTest" runat="server" Width="250px" onkeyup="EnableDisableButton(this,'btnReset')"></asp:TextBox>
        <asp:Button ID="btnReset" runat="server" Text="Reset" Font-Bold="true"
            Enabled="false" onclick="btnReset_Click" />
    </fieldset>       
    </div>

  • In the Code behind file (default.aspx.cs) write the code as:

protected void btnReset_Click(object sender, EventArgs e)
    {
        txtTest.Text = string.Empty;
    }

Note: I have enabled and disabled the button control but you can do it with any other asp.net control as per requirement. You just need to change the Id of the "btnReset" with the id of any other control you want to enable or disabled in the Above javaScript function.

 Asp.Net VB Section
  •  In the <Form> tag design the asp.net page e.g. default.aspx as:
<div>
    <fieldset style="width:320px">
    <legend>Enable/Disable asp.net control on typing in textbox</legend>
    <asp:TextBox ID="txtTest" runat="server" Width="250px" onkeyup="EnableDisableButton(this,'btnReset')"></asp:TextBox>
        <asp:Button ID="btnReset" runat="server" Text="Reset" Font-Bold="true"
            Enabled="false" />
    </fieldset>       
    </div>

  • In the Code behind file (default.aspx.vb) write the code as:
Protected Sub btnReset_Click(sender As Object, e As System.EventArgs) Handles btnReset.Click
        txtTest.Text = string.Empty 
    End Sub

Now over to you:
" I hope you have got How to enable/disable asp.net controls on textbox value change or typing text in Asp.Net using regular expression and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates."
Previous
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..