Loading .NET User Controls at Run Time

.NET user controls are great, but while you may always want to see a header or footer, some user controls you may want to load only in some circumstances. Here’s one way to do it.

1. Put a place holder on your page for the control.
<asp:PlaceHolder ID=”phDataArea” Runat=”server”></asp:PlaceHolder>

2. Add a user control to the place holder programmatically.
Dim ucSearch As Control = LoadControl(“uc_search.ascx”)phDataArea.Controls.Add(ucSearch)

In this way you can save a space to load a variety of different user controls on the fly. It’s a good way to keep your main page clean and to make it easier to have multiple people working on the same project.