Getting rendered HTML of a server control

This post shows a generic method that can return the HTML string generated by ASP.NET server control after the rendering.

string GetRenderedOutput<T>(T control) where T : Control
{
    StringBuilder builder = new StringBuilder();
    using (StringWriter sWriter = new StringWriter(builder))
    using (Html32TextWriter writer = new Html32TextWriter(sWriter))
    {
        control.RenderControl(writer);
    }
    return builder.ToString();
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s