Why does a read-only textbox not return any data in ASP.NET?

input picture input · May 7, 2009 · Viewed 17.2k times · Source

I've set a textbox to read-only. When the user clicks on it, a calendar is displayed and the user selects the date which inputs into the read-only textbox.

But when I try to enter the data into the database, it shows null value. What is wrong?

Answer

Andrew Hare picture Andrew Hare · May 7, 2009

There is a little bit of strangeness when it comes to the ASP.NET Readonly property and the readonly attribute of an HTML input element. Rather than setting the Readonly property of the web control try simply adding the HTML attribute to the control like this:

textBox.Attributes.Add("readonly", "readonly");

This will make the control read-only in the client's browser yet still allow you to retrieve the value of the input when it posts back to the server.