views:

466

answers:

7

I was recently trying to explain to a programmer why, in ASP.Net, they should create HTMLControls instead of creating HTML strings to create Web pages.

I know it is a better way of doing things, but I really couldn't give concrete reasons, other than, "This way is better."

If you had to answer this question, what would your answer be?

Why is

Dim divA as New HtmlControls.HtmlGenericControl("div") 
Dim ulList1 as New HtmlControls.HtmlGenericControl("ul") 
Dim liObj1, liObj2, liObj3 as New HtmlControls.HtmlGenericControl("li") 
liObj1.innerText = "List item 1" 
liObj2.innerText = "List item 2"
liObj3.innerText = "List item 3"
ulList1.Controls.Add(liObj1)
ulList1.Controls.Add(liObj2)
ulList1.Controls.add(liObj3)
divA.Controls.add(ulList1)

"better" than:

Dim strHTML as String
strHTML = "<div><ul><li>List item 1</li><li>List item 2</li><li>List item 3</li></ul></div>"

? It doesn't look better. Look at all that code! And, this is a very simplistic example, to save space. I don't think I would ever actually create a list manually like that, but would be iterating through a collection or using a more advanced Web control, but I'm trying to illustrate a point.

A: 

It's only REALLY better in two cases:

  1. You need to do something with those controls in your code
  2. The resulting manual HTML is such a mess that it's impractical to maintain as a string.

From a code perspective, neither is a better solution. They both work perfectly fine as far as display goes.

DannySmurf
+2  A: 

I don't do either. Instead, I:

  1. Create a custom control that encapsulates the desired construct
  2. or (very similar) create a simple class with the properties I want and override the .ToString() method to create the desired HTML.

But of your two choices, a couple reasons the former is better are:

  • You can change properties of html controls or add other controls to the tree at later points in the page life cycle
  • ASP.Net is responsible for rendering the html. You're guaranteed not to have any mistakes that might break xhtml compliance.
Joel Coehoorn
+2  A: 

One big advantage: Writing HTML as a string is hugely prone to human error, and has the potential to get unreadable and unmanageable very quickly.

Generated HTML is going to be considerably better protected from humans and is self documenting.

(For the record I despise both methods, and use XSLT for my templating)

annakata
+1  A: 

You might prefer a template system as most modern web development frameworks provide. I am unsure of the options available for ASP, but there must be a few. Using a template system allows you to refrain from placing any markup in your code or write verbose document construction blocks, as in the two examples you provide in your question.

Try googling "asp.net templates templating" for a place to start. It would be nice if there was a page such as the following for asp.net. Let us know if you find one:

http://wiki.python.org/moin/Templating

naturalethic
A: 

Personally, I would only create controls if I had to since alot of Microsoft's HTML only works in quirks mode, and I agree that creating HTML strings is VERY prone to error.

I create XHTML with an XML parser, and then grab the .toString from the XMLDocument object. It is verbose, but the output is always properly formatted, and always works.

Jeff.Crossett
A: 

The benefit is that the code that is generated by ASP.NET is rendered differently under different conditions. Browsers versions, software updates/patches, trends, additionally frameworks, etc. When you write the HTML yourself and just dump it to the screen, it is does not change, ever. It will always be what you write it to be.

But, if you add it using .NET Objects (the recommended way), the actual code that is rendered in the end user's browser is created during runtime. A good example where this is using the "CSS Control Adapter Toolkit for ASP.NET 2.0" which changes the way .NET Controls render, changing the default method to render controls to use pure CSS.

A: 

Building up controls like you have is The Single Worst Thing About ASP.NET. An MVC or approach with Partials are a much better(more easily maintained) way to construct HTML.

Mark