A ReadOnly ASP.Net TextBox that can PostBack data

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"

Leave a comment

Please note that we won't show your email to others, or use it for sending unwanted emails. We will only use it to render your Gravatar image and to validate you as a real person.