Get Rendered Html From a Control

Whilst working on the CMS I needed to be able to pass back the html a control will render when the user first adds it. The control doesn’t exist on the page yet as the sections are added via javascript. It was very easy to pass in StringWriter to the render function to get the html 🙂

public string GetControlHtml(Control ctrl)
//Create StringBuilder to get writer from
StringBuilder sb = new StringBuilder();
//Get the writer
StringWriter sw = new StringWriter(sb);
//Create a HtmlTextWriter using our StringBuilder as stream
HtmlTextWriter htw = new HtmlTextWriter(sw);

return sb.ToString();

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.