Home > ASP.Net, Javascript > Enable and Disable of Textbox through a Checkbox

Enable and Disable of Textbox through a Checkbox


Hi all,

try this example to Enable and disable of text box through a checkbox using Javascript

<head runat="server">
    <title>Untitled Page</title>
<script language="javascript" type="text/javascript">
    function enable_text(status)
    {
    status=!status;
    document.getElementById('<%= txt_Name.ClientID %>').disabled = status;
    document.getElementById('<%= txt_Name.ClientID %>').value='';
    }
</script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:CheckBox ID="CheckBox1" runat="server" onclick="javascript:enable_text(this.checked);" />Others
    <asp:TextBox ID="txt_Name" runat="server"></asp:TextBox>
    </form>
</body>

and in code behind we will add code to disable TextBox till Checkbox checked

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txt_Name.Attributes.Add("disabled", "disabled");
        }
    }

Hope this helps

Good Luck

Advertisements
Categories: ASP.Net, Javascript
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: