views:

46

answers:

3

I was wondering, whats the best way to handle common HTML controls in ASP.NET? I mean, ASP.NET server controls generate too much crap inside the code so I rather use common controls.

But how about databind and how to manage those common objects correctly (such as combobox, textbox and so on)?

A: 

There's a reason ASP.Net controls generate all that "crap". It's so you can databind and manage those objects correctly. Using standard html controls is useful when you don't need direct databinding or if you have no need to manipulate the control through server-side code.

The best way to handle common HTML controls in ASP.Net is not to handle them directly. By using them to handle data and functionality, you're basically neutering .Net. You might as well go back to classic ASP.

Joel Etherton
I believe that onanon's concern is well founded, i myself am not content with the way the html markup looks on some asp.net pages. And the biggest problem is not that i don't like it, but it can really bloat a page's size, and it's by no means search engine friendly. Microsoft has tried to minimize these downsides with extensions such as it's search engine optimization toolkit, but that's just patching up, not solving the root of the problem.
scripni
@scripni - I agree completely. I really like your idea of using MVC. It's not an ideal solution because a bloated application can still lead to bloated html. That doesn't change how .Net operates though. As I'm reading it he's saying "I really like how powerful my car is, I just wish it didn't make so much noise."
Joel Etherton
+1  A: 

If you're concerned about the html markup generated by the WebForms ASP.NET engine, i suggest you take a look at ASP.NET MVC. It's purpose is specifically to give you the control you need over the generated html. If you don't want to start learning ASP.NET MVC, ASP.NET 4.0 WebForms gives you more flexibility in the generated HTML (such as enabling the ViewState for a specific control only, setting the html id's etc.).

As for the databinding, again if you study MVC in depth and start thinking in terms of action -> result you can gain a lot more control and flexibility.

Later edit: I forgot to mention Razor, the new ViewEngine under development at microsoft. It's currently in beta and only inside WebMatrix, a very stripped down 'getting-started type' development platform for ASP.NET. MVC combined with the very clean code you can write using Razor will be in my opinion an important trend-setter in the web development world.

scripni
+2  A: 

Don't forget that you can always set runat="server" on any control - that includes standard html form controls such as <input> and <select>, and also other elements like <div>. Anything, really.

This means that you can regain control of the html output in your WebForms pages quite effortlessly - as long as you don't need viewstate or any other more advanced databinding/state managing that ASP.NET normally handles for you.

That said, learning to use the ASP.NET MVC Framework is not a bad idea, since it helps you regain control of much more than just the html output. Generally, creating a page in ASP.NET MVC takes a little more work, since there are no drag-n-drop controls like gridview, textbox or even repeater. Instead, you use html helper methods and regular foreach loops, which means you have to type a lot more code. However, the MVC framework is designed so that you won't have to repeat much code anyway.

Tomas Lycken