Anatoly Lubarsky

Logo
MSSQL, .NET, Design. Life and Music

ASP.NET Tip: Render Control into HTML String

Occasionally there is a need to get string representation of ASP.NET control in other words - render it into string instead of let it be rendered on the page. The following method renders control into HTML string. Namespaces used:


using System.Text;
using System.IO;
using System.Web.UI;

Below is RenderControl method implementation, which receives any control and returns its HTML string representation.


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

via aspnetmania.com forums.


Related Posts:

Saturday, March 26, 2005 8:04 AM

Comments

# re: Asp.Net: Render control into HTML string
You forgot the semicolon on your return line :o

2/1/2007 3:50 PM by Bob

# re: Asp.Net: Render control into HTML string
Bob: fixed, thanks

2/2/2007 3:05 AM by Anatoly Lubarsky

# re: ASP.NET Tip: Render Control into HTML String
when I call ctrl.RenderControl it did not call ctrl.Page_Load

6/8/2007 1:28 PM by lam

# ASP.NET Tip: Render Control into HTML String
You've been kicked (a good thing) - Trackback from DotNetKicks.com

11/13/2007 2:13 PM by DotNetKicks.com

If your feedback doesn't appear right away, please be patient as it may take a few minutes to publish.

Post a Comment

Protected by CAPTCHAEnter the code you see
Name (*)  
E-mail (*)  
Url
Remember

Comment (*)