HtmlTextWriter to string

If you create custom controls in ASP.NET, you will probably, sooner or later, be dealing with HtmlTextWriter. This tool allows you to generate HTML tags. It is better, comparing to StringBuilder, because it allows to build a nicely indented markup.
In this example, we'll show you how to debug it, in order to explore its content.

programming

Use the following method.

 private string GetString(HtmlTextWriter writer)
{
StringBuilder sb = new StringBuilder();
HtmlTextWriter htw = new HtmlTextWriter(new System.IO.StringWriter(sb,

System.Globalization.CultureInfo.InvariantCulture));
foreach (Control ctrl in Controls)
{
ctrl.RenderControl(htw);
}
string strContents = sb.ToString();

return strContents;
}

Print Friendly, PDF & Email

Leave a Reply