Tuesday, August 26, 2008

ASP.NET MVC HtmlHelper ReadOnly TextBox

If you've been working with ASP.NET MVC Preview 3, or later, you may have wondered how to create a readonly textbox control using the HtmlHelper class.

You can accomplish this using an anonymous object initializer with the property "ReadOnly" for the htmlAttributes parameter.

Here is an example:
Html.TextBox("UrlID", new {ReadOnly="ReadOnly"})

You can also specify other arguements for the rendered Html input control as part of the htmlAttributes parameter, just by including them in the anonymous object initializer.

Here are a few other examples to help you out:
new { _class="cssClass", onclick="alert('Alert!')" })


Antonio said...

This post is worthless without more sourcecode!!!

Christopher Hardwick said...

Harsh Antonio, harsh.

Ron said...

Actually, I found it to be exactly what I needed.