asp:Textbox – the difference between ReadOnly and Disabled

Again this may be something that is old news to some of you, and perhaps it is to me as well and I had forgotten, but today I was hugely frustrated with .NET seemingly ignoring some perfectly populated fields and the validators reporting that they were empty.

The fields in question had been populated with values from the database after a choice was made from a combobox and a quick AJAX call. When trying to submit, as mentioned, the specified field’s validators were complaining that they were missing. Which, as it turned out, to all intents and purposes, they were.

Why?

Well when I a choice was made from the combobox, I was setting the fields-to-populate to disabled, so they would be greyed out and the user would be unable to alter them. But this causes the browser to not post their values when submitting the form. Setting the ‘readonly’ attribute rather than the ‘disabled’ one effectively has the same effect except the values get posted when the form is submitted.

So remember folks:

document.getElementById('identifier').readOnly = true;

is the way to get over this.