Should I use HtmlHelper or roll my own controls?

I'm just beginning to learn ASP.NET MVC and I've run into a question. I'm trying to determine whether I should use HtmlHelper to create client controls or if I should just roll my own. My gut wants to lean towards just rolling my own because it gives me total control - and use jQuery to decorate and add cross-browswer functionality. But then I can see advantages of using HtmlHelper for various complex controls that may involve things like paging.

I'm looking for experiences about when it was better to use HtmlHelper and when it was better to roll your own.

Answers


The more they add to HtmlHelper the more I end up using them myself.

Take a look at these posts from ScottGu: MVC Preview 5 and MVC Beta

If you don't use the HtmlHelper versions of the input boxes, you won't get the nice, free validation.

Also, I'm not sure what prevents you from decorating the HtmlHelper controls with whatever you need to make them usable for jQuery.

You can add whatever attributes you need using the htmlAttributes overload like so:

<%= Html.TextBox("LastName", ViewData.Model.LastName, new { @class = "required" })%>

The interesting part for what I'm talking about is the anonymous object you see there (new { @class = "required" }).

You can put anything in there you'd need, and it gets slapped onto the attribute section of the control.

Good luck!


Rolling your own is pretty easy for the easy cases, but it gets harder the more "magical" you want them to be.

Luckily you can build the as you go.

The provided HtmlHelper controls are pretty extensive, however. They'll provide most of what you'll need. Just be cautious about the overloads, as it's very easy to accidentally use the wrong overload for your calls.


The real question you need to ask yourself is, do you need total control, do you just need a working control that gets the job done in the fastest time possible (this doesn't mean you have to sacrifice quality).

If speed is an issue, use HtmlHelper, if you want to play in control land, then go with rolling your own. If you're not sure, go with HtmlHelper and save homebrewed controls for your spare time experimentation.


I created a fluent interface for HTML, and I have decided to share it here. Maybe you will find it a good alternative. I also wrote a blog post about it.

Here's a teaser:

<%=this.TextBox(x => x.FirstName).Class("required").Label("First Name:")%>
<%=this.CheckBox("enabled").LabelAfter("Enabled").Title("Click to enable.").Styles(vertical_align => "middle")%>

Critique and contributions are welcome.


Need Your Help

Animation List simply deosn't start

android animation

I'm working on a project which should have a progressdialog. But since i didn't find an easy way to style a progressdialog, i was thinking, that the easiest way is to create a custom dialog class w...

Prevent contenteditable element from getting focus when clicking parent

html events focus contenteditable

When clicking anywhere above the "Outside" div container in the following example, the contenteditable span element gets the focus.