Sunday, December 05, 2010

ASP.NET Tip: Render Control into HTML String

How to get string representation of ASP.NET control or in other words render it into string instead of let it be rendered on the page. Here is the sample code to Render control

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();
}

No comments:

Post a Comment