Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Monday, 2 March 2015

Introduction: In this article I will explain how to enable or disable asp.net controls on textbox value change or typing text in TextBox or in other words we can say enable and disable  any asp.netcontrols like TextBox, Label, DropDownList, RadioButton, RadioButtonList, CheckBox, CheckBoxList etc as per requirement on  TextBox control value change in asp.net using Javascript. 
For Example Suppose we want to enable the Clear button  on textbox  value change that clears the textbox when clicked. But on page load by default this button should be disabled , because there is no any value in textbox so no need to clear textbox. Whenever we enter any character  then the reset button should be enabled so that if required we can clear out the values we entered.
This can be done on 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.

Source Code:
In the <Head> tag of the design page,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:

 <div>
    <fieldset style="width:370px">
  <legend><strong>Enable/Disable asp.net control on typing in textbox</strong></legend>
    <br /><br />
    <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;
    }

You can disable/Enable  any other asp.net control as per requirement. Just change the Id of the "btnReset" with the id of any other control you want to enable or disabled in the 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 write the code as:

Protected Sub btnReset_Click(sender As Object, e As System.EventArgs) Handles btnReset.Click
        txtTest.Text = string.Empty 

    End Sub
Categories: , ,

0 comments:

Post a Comment