Sjoemeleboem Software

Home of Paul Deen

VB, .NET, SQL Server, MOSS

Programming for life!

Serving the web community
with my knowledge

››› See the latest news

Persist Viewstate for disabled textboxes

When you make an ASP.Net textbox disabled by setting the Enabled property to false, the Text property is not persisted in ViewState. This is a bug in Internet Explorer. But anyway, I just want this problem to be solved, so I made a workaround.

The solution

You need to set the ReadOnly property instead of the Enabled property. Furthermore, you could change the display style (make the text gray) by using the Style property.

Example code (C#)

protected System.Web.UI.WebControls myTextBox;

private void Page_Load(object sender, System.EventArgs e)
{
myTextBox.ReadOnly = true;
myTextBox.Style["color"] = "gray";
}

Example code (ASPX)

<asp:textbox runat="server" id="myTextBox" ReadOnly="True" ForeColor="gray" />

Example code (javascript)

document.getElementById('myTextBox').readOnly = true;
document.getElementByid('myTextBox').style.color = 'gray';

Last modified on: 2005-09-22