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.


Use the following method.

 private string GetString(HtmlTextWriter writer)
StringBuilder sb = new StringBuilder();
HtmlTextWriter htw = new HtmlTextWriter(new System.IO.StringWriter(sb,
foreach (Control ctrl in Controls)
string strContents = sb.ToString();

return strContents;

Print Friendly, PDF & Email

Comments are closed.