tags:

views:

407

answers:

2

Custom controls can use this for example to render out a span tag:

writer.RenderBeginTag(HtmlTextWriterTag.Span);  
writer.Write(Text);  
writer.RenderEndTag();

Ok, why can't I just do this:

writer.Write("<span>");
writer.Write(Text);
writer.Write(</span>");

this way I can actually see it rather than reading HtlmlTextWriter tags all over the place and also tweak any of the mark-up easily such as adding cs classes, ids, or whaetver to the tags. And if you say it's faster to type because of Intellisense, is that about the only reason to use RenderBegin and RenderEnd? that's not much of a case.

A: 

RenderBeginTag will add all attributes that you have added prior to calling it. So it is an easy way to add attributes dynamically when writing out the html.

Daniel
why can't I just write out a stringbuilder that adds attributes dynamically and then ultimately write out the string from the stringbuilder?
CoffeeAddict
I guess I don't like using this syntax...it seems so proprietary and I like to see the actual mark-up so I can work with it in the custom control easier and visualize it better.
CoffeeAddict
+2  A: 

You will have less typos (since "<spsn>" won't give a compile error, but HtmlTextWriterTag.Spsn will).

And perhaps not in this case, but using defined Tags will make it easier to apply changes later in code. If for some strange reason, span will be named something else, the value of the tag Span can easily be changed at one place, effectively changing all places, instead of manually changing every place where you used the string value...

Patrick
Thanks Patrick, while valid points I tend to disagree. I think that developers should use mark-up and be responsible for it. Ignoring mark-up and delegating it to some method to me is pointless. We as developers especially need to become better at mark-up, especially moving to tableless design. Its because we refuse to change to tableless that our design becomes rigid for the rest of the business.
CoffeeAddict