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? I use and recommend a Digital Ocean SSD VPS server! Check out my referral link below:

Get an awesome virtual SSD server cheap!
blog comments powered by Disqus