Home > ASP.NET, c# > Rendering the HTML Output of a Server Control

Rendering the HTML Output of a Server Control

I recently found a neat trick to render the output of a control without actually having to add the control itself to a page or control. I found this very handy when trying to use a site template that everything must fit in, and the controls may have various locations within the page that they need rendered. In my case, I had HTML graphics comps that had specific spots in it that I would need to place specific controls. The graphics comps could be swapped out on the fly, and the controls would need to be rendered in different places based on which graphics comp was used. See what I did here to render the control HTML within the “CONTENTHERE” placeholder of the graphics design comp HTML.

ThemeObject to = bl.getTheme(siteID);

StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter tw = new HtmlTextWriter(sw);

GalleryControl gallery = new GalleryControl();
gallery.ID = “gallery”;    
gallery.RenderControl(tw);
sHTML = sb.ToString();
sHTML = to.Body.Replace(“CONTENTHERE”, sHTML);

Categories: ASP.NET, c# Tags:
  1. No comments yet.
  1. No trackbacks yet.