ASP.NET Tip: Render Control into HTML String
Occasionally there is a need to get string representation of ASP.NET control in other words - render it into string instead of let it be rendered on the page. The following method renders control into HTML string. Namespaces used:
using System.Text; using System.IO; using System.Web.UI;
Below is RenderControl method implementation, which receives any control and returns its HTML string representation.
public string RenderControl(Control ctrl) { StringBuilder sb = new StringBuilder(); StringWriter tw = new StringWriter(sb); HtmlTextWriter hw = new HtmlTextWriter(tw); ctrl.RenderControl(hw); return sb.ToString(); }
via aspnetmania.com forums.
Saturday, March 26, 2005 8:04 AM