How to make the TextBox readonly or disabled in ASP.NET

You can make the TextBox control readonly by setting its ReadOnly property to true. You can set this property in the .aspx page which contains the TextBox control, or programmatically from the code-behind. The next test .aspx page shows this:

ReadOnlyTextBox.aspx

<%@ Page Language=”C#” AutoEventWireup=”true” CodeBehind=”ReadOnlyTextBox.aspx.cs”

Inherits=”TextBoxSamplesVC.ReadOnlyTextBox” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

<html xmlns=”http://www.w3.org/1999/xhtml”>

<head runat=”server”>

<title> ReadOnly TextBox</title>

</head>

<body>

<form id=”form1″ runat=”server”>

<div>

<asp:TextBox ID=”tbTest” runat=”server” Text=”Try to change me” ReadOnly=”true” ></asp:TextBox>

</div>

</form>

</body>

</html>

 

You can make the TextBox disabled by settting its Enabled property to false. You can set this property in the .aspx page which contains the TextBox control, or programmatically from the code-behind. The next test .aspx page shows this:

 

ReadOnlyTextBox.aspx

<%@ Page Language=”C#” AutoEventWireup=”true” CodeBehind=”ReadOnlyTextBox.aspx.cs”

Inherits=”TextBoxSamplesVC.ReadOnlyTextBox” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

<html xmlns=”http://www.w3.org/1999/xhtml”>

<head runat=”server”>

<title>ReadOnly TextBox</title>

</head>

<body>

<form id=”form1″ runat=”server”>

<div>

<asp:TextBox ID=”tbTest” runat=”server” Text=”Try to change me” Enabled=”false” ></asp:TextBox>

</div>

</form>

</body>

</html>