I have a TextBox control and I want the rendered input element to have the readonly attribute with value readonly:
<input type="text" readonly="readonly" />
I could do it in code-behind using the following code:
textBox.Attributes["readonly"] = "readonly"
but I’d rather have all my control attributes and their values in my markup. Can I do it in the markup?
A solution could be to extend the TextBox control, but this feels overly complicated for the intended result.
I want to be able to change the value client-side with javascript, and read the value server-side during postbacks. Please note that putting the TextBox.ReadOnly property to True would not solve the issue, as this also causes the server to ignore the TextBox’ value on postbacks. (MSDN.) I simply want the readonly behavior from the browser.
It seems you can’t do it cleanly in the markup without getting ASP.NET’s
ReadOnlyside effects.However, you could use inline code, although it looks ugly.