Dynamic Loading of ASP.NET User Controls
page 1 of 1
Published: 16 Nov 2004
Unedited - Community Contributed
Demonstrates how to dynamically add user controls to pages at runtime using the LoadControl method.
by Brett Burridge
Average Rating: 
Views (Total / Last 10 Days): 25371/ 38

User Controls are semi-autonomous pages of HTML and underlying ASP.NET code that can be inserted into ASP.NET pages. As such they are useful for adding blocks of functionality to pages. Typical uses are to use User Controls for page headers and footers. They can also add functionality such as a "property of the week" for a real estate company website.

Unlike ASP.NET pages that have the .aspx file extension, User Controls typically have the .ascx file extension. Once created, in Visual Studio.NET they can be included in a specific page by simply dragging the user control onto the design view of that page. Alternatively, they can be added to a page at design time by including the following in the page's HTML. For example, the following line includes a header user control from the HeaderUserControl.ascx file:

<%@ Register TagPrefix="uc1" TagName="HeaderUserControl" Src="HeaderUserControl.ascx" %>

The header is then positioned on the page using the following tag:

<uc1:HeaderUserControl id="Header1" runat="server"></uc1:HeaderUserControl>

Although this procedure is satisfactory for content like headers and footers that will always be required on specific pages, it would be useful if there was a way of dynamically loading specific user controls at run time. For example, an online store may only want a "clearance offers" section to appear if there is actually stock inventory in the database that has been marked as being for clearance.

Fortunately, it is possible to load user controls onto a page by making use of the LoadControl method. This function has a straightforward syntax - it takes a single argument - the virtual path to the user control page. For example, to load the featured product user control the following C# code would be used within the Page_Load method:

Control FeaturedProductUserControl = LoadControl("FeaturedProduct.ascx");

Once the user control has been loaded, it can be added to the page by adding it to the Controls collection:


The drawback with this technique is that it offers no control over where on the page the user control will actually appear. A useful tip is, therefore, to add a place holder control to the page in the position that you want it to display the dynamically loaded user controls. A place holder does just that - it acts as a container for other controls. You can then specify that the user control appear within the place holder by adding the user control to the place holder's controls collection:


Alternatively it is possible to position the user control in other ways, such as adding it to a Panel control:


Making Full Use of Dynamically Loaded User Controls

Since it is possible to dynamically load user controls onto specific pages, it provides the idea functionality for creating page templating facilities within ASP.NET. For example, by storing the details of available user controls in a database, it would be possible to write a content management system that allowed the website's administrative users to choose which user controls they wanted to display on each page. This page templating system is used in content management systems such as the portal layout functionality in Microsoft's SharePoint Portal Server. The ASP.NET IBuySpy sample projects also make use of the LoadControl method to dynamically load content.

Further Reading

Having trouble with the documentation of your ASP.NET web applications? The ASP.NET Documentation Tool can create comprehensive documentation for ASP.NET projects written in VB.NET and C#. Reports are created in standard HTML, Microsoft HTML Help and plain text formats.


User Comments

Title: Event handling in Dynamically Loaded Controls   
Name: AA
Date: 2005-06-08 10:13:01 PM
The code is fine, in fact similar examples are available on other sites, but this doesn't seem to work if you have event handlers for controls inside the user control! The events are not caught properly! Any suggestions would be appreciated. Cheers.
Title: Great   
Name: Mike
Date: 2005-06-02 1:15:32 PM
Just wat i needed ;-)
Title: alo   
Name: alo
Date: 2005-05-22 5:30:29 AM
i need more code and examples, if i was that professional i won't ask for help
Title: Solution to accessing properties dynamically   
Name: Daniel C. Douglass
Date: 2005-05-20 12:25:31 PM
Class name of Custom Control: New_Vote
Public Properties (in New_Vote): DirectorName, VoteID
Custom Control Filename: New.Vote.ascx

The following code should be placed in the code-behind of the web form calling the user control:

//### load user control
Control c = LoadControl("Controls/New.Vote.ascx");

//### cast the generic control to be voting control
New_Vote Vote = (New_Vote)c;

//### set control properties
Vote.DirectorName = "Daniel C. Douglass";
Vote.VoteID = 117;


This should help everyone!
Title: Dynamic Properties of Custom Control   
Name: Daniel Douglass
Date: 2005-05-20 11:59:55 AM
I found this article somewhat helpful. I was able to easily implement the dynamic control, however I have had issues accessing the properties of this control dynamically. I tried the previously suggested methods, to no avail. Does anyone have any suggestions on how to accomplish this using c#?
Title: dynamic loading user control   
Name: Mag
Date: 2005-05-06 3:08:36 AM
I tried the above but using VB.NET's syntax.
The user control does not appear.
I also tried adding it to a panel or placeholder. It just does not appear.
Anybody know the solution?
Title: dynamic loading   
Name: Arif
Date: 2005-03-05 6:51:37 AM
This is a superb help about loading user controls dynamically. But this help must include the drawbacks of loading usercontrols dynamically. Regards.
Title: Properties Use   
Name: Spent Too Much Time On This
Date: 2005-03-01 4:24:57 PM
I use VB instead of C# so the syntax may be different but I have had success in using the properties of dynamically added user controls by changing this code (assume the new user control = FeaturedProduct from the example above):

Old Code= Dim FeaturedProductUserControl as UserControl = LoadControl("FeaturedProduct.ascx")

New Code = Dim FeaturedProductUserControl as FeaturedProduct = LoadControl("FeaturedProduct.ascx")

That seems to work for me - provides intellisense and access to my user control's properties.
Title: Dynamic Control Loading (maybe not so dynamic?)   
Name: Rich
Date: 2005-02-24 11:19:16 AM
"It contains basic to load control dynamically.. But doesnt specify how to use properties in control
if it is loaded dynamically."

I second this last comment. This tutorial is worthless if you aren't able to manipulate properties of the loaded control. Otherwise, I'd have to have THOUSANDS of controls in subfolders waiting to be loaded whereas if I could manipulate the properties of the loaded control I would just need one or two. PLEASE explain how to do this!! I BEG OF YOU!! :)
Title: Dynamic Loading of ASP.NET User Controls   
Name: Nilay
Date: 2005-02-22 12:22:30 PM
It contains basic to load control dynamically.. But doesnt specify how to use properties in control if it is loaded dynamically.
Title: Dynamic Loading of ASP.NET User Controls   
Name: Sudha
Date: 2005-02-04 5:20:34 AM
Not providing sufficient information
Title: Excellent reference   
Name: Arif
Date: 2005-01-22 11:30:44 PM
Thank you. That was just the thing I was looking for. Problem solved.
Title: User Control Feedback   
Name: Jeremy Cummings
Date: 2005-01-19 10:41:39 AM
This is not too helpful. where is the rest of the code??? I want to see FULL examples.
Title: loadcontrol   
Name: ash
Date: 2005-01-12 12:15:11 AM
The article is quite good but i want more information regarding this.
Suppose i have a menu on web form and i want to display different user controls on web form on different menu item click then how can i do that
Title: very good!   
Name: fabrizio
Date: 2005-01-11 12:57:28 PM
very good!
Title: Dynamic Addition   
Name: Todd
Date: 2005-01-03 9:44:47 PM
Great Article
Title: Dynamic Loading of ASP.NET User Controls   
Name: Demonstrates how to dynamically add user controls to pages at runtime using the LoadControl method.
Date: 2004-12-06 8:44:11 AM
Very helpful. The explanation is very precise and clear.

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-05-27 9:00:51 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search