Home > Asp.Net, c#, cms > Get Rendered Html From a Control

Get Rendered Html From a Control

November 16th, 2009 fbis Leave a comment Go to comments

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);

ctrl.RenderControl(htw);
return sb.ToString();
}

Categories: Asp.Net, c#, cms Tags:
  1. No comments yet.
  1. No trackbacks yet.