How to get UserControl HTML output in .NET (C #)?

If I create a UserControl and add some objects to it, how can I capture the HTML that it displays?

ex.

UserControl myControl = new UserControl(); myControl.Controls.Add(new TextBox()); // ...something happens return strHTMLofControl; 

I would like to simply convert the newly created UserControl to an HTML string.

+45
c # user-controls
Nov 13 '08 at 21:31
source share
7 answers

You can do control using Control.RenderControl(HtmlTextWriter) .

StringWriter to HtmlTextWriter .

StringBuilder to StringWriter .

The generated string will be inside the StringBuilder object.

Here is the sample code for this solution:

 StringBuilder myStringBuilder = new StringBuilder(); TextWriter myTextWriter = new StringWriter(myStringBuilder); HtmlTextWriter myWriter = new HtmlTextWriter(myTextWriter); myControl.RenderControl(myWriter); string html = myTextWriter.ToString(); 
+55
Nov 13 '08 at 21:35
source share
 //render control to string StringBuilder b = new StringBuilder(); HtmlTextWriter h = new HtmlTextWriter(new StringWriter(b)); this.LoadControl("~/path_to_control.ascx").RenderControl(h); string controlAsString = b.ToString(); 
+30
May 7 '09 at 2:58 a.m.
source share
 UserControl uc = new UserControl(); MyCustomUserControl mu = (MyCustomUserControl)uc.LoadControl("~/Controls/MyCustomUserControl.ascx"); TextWriter tw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(tw); mu.RenderControl(hw); return tw.ToString(); 
+12
Jan 23 '11 at 23:10
source share

Seven years late, but it deserves to be shared.

The common solution - StringBuilder in StringWriter in HtmlWriter in RenderControl - is good. But there are some gotchas that I, unfortunately, have come across while trying to do the same. Some controls will throw errors if they are not inside the Page , and some will throw errors if they are not inside the <form> with runat="server" . The ScriptManager control demonstrates both of these behaviors.

In the end, I found a workaround here. Its essence is mainly to create an instance of a new page and form before performing the author’s work:

 Page page = new Page(); page.EnableEventValidation = false; HtmlForm form = new HtmlForm(); form.Name = "form1"; page.Controls.Add(form1); MyControl mc = new MyControl(); form.Controls.Add(mc); StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); HtmlTextWriter writer = new HtmlTextWriter(sw); page.RenderControl(writer); return sb.ToString(); 

Unfortunately, this gives you more markup than you really need (since it includes a dummy form). And the ScriptManager still fails for some secret reason that I haven't puzzled yet. Honestly, this is a lot of trouble and is not worth doing; the whole point of generating controls in the code lock is such that in the end you don’t have to bother with markup.

+8
Jul 24 '15 at 21:03
source share

override the REnderControl method

 protected override void Render(HtmlTextWriter output) { output.Write("<br>Message from Control : " + Message); output.Write("Showing Custom controls created in reverse" + "order"); // Render Controls. RenderChildren(output); } 

This will give you access to the writer on which the HTML will be written.

You can also learn about the asp.net adaptive management architecture the adaptive asp.net control architecture , where you can "format" the default html output from controls.

+4
Nov 13 '08 at 21:36
source share

Call the .RenderControl() method.

+1
Nov 13 '08 at 21:34
source share

You can use the HttpServerUtility.Execute method, available through HttpContext.Current.Server.Execute :

 var page = new Page(); var myControl = (MyControl)page.LoadControl("mycontrol.ascx"); myControl.SetSomeProperty = true; page.Controls.Add(myControl); var sw = new StringWriter(); HttpContext.Current.Server.Execute(page, sw, preserveForm: false); 

The advantage is that you also fire the Page_Load event of your user control.

MSDN documentation can be found here: https://msdn.microsoft.com/en-us/library/fb04e8f7(v=vs.110).aspx .

0
Feb 01 '17 at 14:22
source share



All Articles