A ReadOnly ASP.Net TextBox that can PostBack data

Posted by Blake on 3/3/2011
)

The ASP.Net TextBox contains a ReadOnly property. The only problem is, this value won’t PostBack to the server if it’s set to true which can be an issue in some cases. If you want to make a TextBox look readonly and not be editable but still post back, try this method by adding attributes to the TextBox via the code behind. The style can be added through the markup but I went ahead and added the readonly attribute through the code behind because there is an actual ASP.NET property of the same name that it conflicts with if you try to add it via the markup.

    txtMain.Attributes.Add("readonly", "readonly")
    txtMain.Style.Add("color", "#CCCCCC")
    txtMain.Text = "This is a test"