Thursday, March 29, 2012

Manually create HTML in User Control

In a few instances, I need to generate HTML manually in my user controls. What I normally do is create this HTML manually in the code behind (it is very complex) using a string builder, and then just set the text value of a literal control to the resulting string of HTML. I was wondering if there is any benefit, or if it is better practice to just override the render method in the code behind and write my HTML using the HTMLTextWriter object. Thoughts?

bounce

i thinkkk

i have heard your name somewhere before


Have you considered creating a Web User Control instead? You can write the HTML once (and include nested ASP.NET controls) and use it wherever you like.


I am using a user control. The question was regarding the best practice for manually rendering HTML in a user control, without the use of an existing templated server control.

I would think that using the HtmlTextWriter in your control's Render() method would be better than adding a literal control.

By using a literal control nested in your control, the literal control's Render() method will have to be called in addition to your control's Render() method which reduces your codes overall efficiency.

0 comments:

Post a Comment