by Phil Adams
6. July 2009 13:50
Occasionally there is a need to get string representation of ASP.NET control in other words - render it into string instead of letting it be rendered on the page. The following method renders a control into its HTML string.
Namespaces used:
using System.Text;
using System.IO;
using System.Web.UI;
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();
}