Monday, November 05, 2007 8:30 PM srkirkland

Dynamically Render A Web User Control

I recently ran into a situation where I wanted to render the contents of a user control either directly to the current HTML stream or to a file format like MS Word. Fortunately .NET has the 'LoadControl' function that takes the virtual path of a User Control and returns a Control object. In order to get the rendered output of a user control into an HTML32TextWriter, you can use the following code:

Control report;
 
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
Html32TextWriter hw = new Html32TextWriter(sw);
 
report = LoadControl("WebUserControl.ascx");
report.RenderControl(hw);

One word of caution: When loading a Web User Control through the LoadControl() method, the normal page lifecycle events, such as Page_Init and Page_Load, are not called. To make up for this deficiency, you can cast the returned Control object to the User Control type and call any required methods directly from the object reference before rendering the control. For example, I created an IReportUserControl interface that defines a LoadReport() method, and which all report-type Web User Controls derive from. Then I can simply insert the following code in order to run the proper initialization logic:

report = LoadControl("WebUserControl.ascx"); 
 
((IReportUserControl)report).LoadReport(); //Initialize the control 
 
report.RenderControl(hw);
Filed under: ,

Comments

# re: Dynamically Render A Web User Control

Tuesday, November 06, 2007 1:14 AM by Eduardo Costa

Nice tip, Scott.

This is something that I'll use really soon.

Thanks!

Eduardo Costa.

# re: Dynamically Render A Web User Control

Tuesday, November 06, 2007 2:07 AM by Dmytro Shteflyuk

You can do something like this before calling RenderControl:

Page page = new Page();

page.Controls.Add(report);

In this case Init method will be called.

# re: Dynamically Render A Web User Control

Wednesday, November 21, 2007 10:23 PM by Peter

Can you give more detail on your IReportUserControl interface and LoadReport function please.

# re: Dynamically Render A Web User Control

Monday, November 26, 2007 1:18 PM by srkirkland

@Peter,

   Basically the IReportUserControl interface is just a simple interface with one method: void LoadReport(StringDictionary parameters);.  You can choose how to represent your parameters, but in my case a key/value stringDictionary worked best.

  Once the interface is defined, your ASCX user control should implement it, like so:

public partial class Review_BiographicalReport : System.Web.UI.UserControl, IReportUserControl

  Then the LoadReport function can do any data binding or calculations required.  After that you just load the user control, cast it as an IReportUserControl and call the LoadReport function, so in the above code.

# re: Dynamically Render A Web User Control

Tuesday, March 18, 2008 5:17 PM by Pinal Bhatt

hi, whenever i try to use this code i get error "Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server."

where GridView1 is a gridview control used in .ascx file.

Help Please

# re: Dynamically Render A Web User Control

Friday, June 05, 2009 8:42 AM by shatru

hi,

i'm new to this site and to asp.net too.

i'm having a problem.

I,ve a user cntrol which is displaing a single row of a table in a label control.

Now, i need to render that user control on an aspx page, say n times (depending upon the no. of rows in the table), but dont know how to achive it. any suggestion??

thnx in advance.

# re: Dynamically Render A Web User Control

Thursday, November 05, 2009 3:25 PM by wullie

Nice Scott. I've just recently done something very similar. Although instead of interfaces I used inheritance. I created a control that inherited UserControl and override RenderControl. With this I simply called OnInit, OnLoad and OnRender before calling base.RenderControl. Any control that inherits this can now render the contents to string and still have its lifecycle events executed. You need to make sure you check they have'nt already fired first though otherwise inclduing them in the control tree will cause duplicate calls.

Leave a Comment

(required) 
(required) 
(optional)
(required)