Custom Search
Logiclabz
  • Home
  • C#
  • HtmlTextWriter to String in Asp.net C#

HtmlTextWriter to String in Asp.net C#

  

This following code will help to generate child controls html content into string. First new stringbuilder (sb) is initiated and then temporary htmltextwriter (htw). Children Controls are rendered using "for" loop and stored in htw. Then "sb.ToString()" is used to get string of whole children control content.

protected override void RenderContents(HtmlTextWriter output)
{
   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();
}



  


Leave a reply




Do you like this post?