How To Render An ASP.NET Control To A String

A simple code snippet. I like to use "using" statements to make sure the various resources get cleaned up in a timely fashion.

Here it is:

var newContent = string.Empty;

using (var stringWriter = new StringWriter())
using (var htmlWriter = new HtmlTextWriter(stringWriter))
    foreach (var control in controls)

    newContent = stringWriter.ToString();

return newContent;

Need hosting? Easily deploy an SSD cloud server on Digital Ocean in 55 seconds. Sign up using my link and receive $10 in credit!

blog comments powered by Disqus