asp.net - How to enable and disable a TextBox programmatically

How to enable, disable TextBox programmatically
This example demonstrate you how can we enable or disable TextBox control programmatically. ASP.NET TextBox server control have a property named Visible. This property have two possible values true and false. if you set TextBox Visible property value false then it will hide from web form and if we set the Visible property value true then it will re visible in web page. This is a simple tricks to enable disable TextBox programmatically. because hidden TextBox control basically act as a disable control.
TextBoxDisable.aspx

<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        TextBox1.Visible = false;
        Label1.Text = "TextBox Disable";
    }
    protected void Button2_Click(object sender, System.EventArgs e)
    {
        TextBox1.Visible = true;
        Label1.Text = "TextBox Enable";
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to enable, disable TextBox programmatically</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Navy">TextBox Example: Enable, Disable</h2>
        <asp:Label 
             ID="Label1" 
             runat="server"
             Font-Size="Large"
             ForeColor="SeaGreen"
             >
        </asp:Label>
        <br /><br />
        <asp:Label 
             ID="Label2" 
             runat="server"
             Text="Favorite Book"
             >
        </asp:Label>
        <asp:TextBox 
             ID="TextBox1" 
             runat="server"
             >
        </asp:TextBox>
        <br /><br />
        <asp:Button 
             ID="Button1" 
             runat="server" 
             ForeColor="MediumOrchid"
             Text="Disable TextBox"
             OnClick="Button1_Click"
             Font-Bold="true"
             />
        <asp:Button 
             ID="Button2" 
             runat="server" 
             Font-Bold="true"
             ForeColor="MediumOrchid"
             Text="Enable TextBox"
             OnClick="Button2_Click"
             />
    </div>
    </form>
</body>
</html>
Related asp.net example