Blog Stats
  • Posts - 178
  • Articles - 0
  • Comments - 61
  • Trackbacks - 234

 

Server Control Development Tip #6: Love that HtmlTextWriter

You may want to skip this tip if you do not care about optimization in your server controls. Yeah right. When you are parsing out all the data that will go into your string, you may be tempted to distrust the HtmlTextWriter. 'Beware the groooooovvveeee.' (sorry, sideline from one of my favorite kids movies, The Emperor's New Groove. Speaking of kids movies, have you seen the Incredibles? How awesome is that movie? I love it.) The HtmlTextWriter is sequential meaning if you write a table tag, pass the writer off to another function, and then come back to write your closing table tag, all will be well. You do not and should not return a string from a different function and then render than through the writer. The only acceptable exception to this is using the StringBuilder if you must. When you take a string called a and a string called b, and join those strings together, you know have three strings. The StringBuilder circumvents this nasty little overhead.
Another discreet and not so widely exploited values of the HtmlTextWriter are the Public and Protected methods of the devil in question. I will not take the time to list them here, as they are numerous, but their value is unquestionable. And, this leads us to ANOTHER value, if you use the Html enum values with the HtmlTextWriter, you get automatic down-level render support. In laymans terms, if you have a tag that older browsers do not support, you can specify which tag should be shown in this instance.

Feedback

# re: Server Control Development Tip #6: Love that HtmlTextWriter

Gravatar Another advantage of using HtmlTextWriter is when you use HtmlTextWriterTag or HtmlTextWriterAttribute, you get to use the constant values for TABLE, TR, or TD, etc. This is a simple method to improve performance since you are no longer reallocating memory for each new tag name. It's not like you're saving a lot of memory, but when you count all your controls on a page and add up the time it would take, you might squeak out a tad bit of time.

Hey, gotta love that HtmlTextWriter. 4/6/2005 10:10 PM | suman chakrabarti

Post a comment





 

Please add 4 and 4 and type the answer here:

 

 

Copyright © Jason Bentley