How do I make this a readOnly textBox, and the text inside it appear as gray?
<%= Html.TextBox("name")%>
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
If you don’t have an initial value you can as well provide only attributes anonymous object with relevant attribute set as
<%= Html.TextBox("name", null, new { @readonly = true })%>
<!-- or -->
<%= Html.TextBox("name", null, new { @readonly = "readonly" })%>
If you do have a value to set to this text box these two should be
<%= Html.TextBox("name", "Some textbox value", new { @readonly = true })%>
<!-- or -->
<%= Html.TextBox("name", "Some textbox value", new { @readonly = "readonly" })%>
The thing is that such attributes as readonly or hidden should be set without any value (according to specification), but you can’t provide such attributes using standard Html helper methods. The good thing though is that you can set anything as a value. But for the sake of readability and making them meaningful it’s best to set them either to true or attribute name as shown in my example.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0