Dynamic (Programmatic) ASP.NET Nested User Controls

This article will detail how to dynamically add nested user controls (ascx pages) to a standard web page. 

In other words:

StandardPage.aspx
>> ucChild.ascx (added as a standard control)
>>>> ucGrandChild (added dynamically from code behind)
>>>> ucGrandChild2 (added dynamically from code behind)

We needed to add the GrandChildren user control 0 to many times, so the dynamic (programmatic) approach was important

As usual, I'm going to post this code as tersely as possible, but if you have a question please just ask in the comments. 

The result will look like this:

Default.aspx 

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="test._Default" %>
<%@ Register Src="~/ucChild.ascx" TagPrefix="uc1" TagName="ucChild" %>
 
<!DOCTYPE html>
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            Main Content<br />
            <uc1:ucChild runat="server" ID="ucChild" />
            End of main content<br />
        </div>
    </form>
</body>
</html>

ucChild.ascx

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="ucChild.ascx.vb" Inherits="test.ucChild" %>
 
Child content<br />
<asp:PlaceHolder runat="server" ID="Placeholder1"></asp:PlaceHolder>
End of child content<br />

ucChild.ascx.vb

*** this is the magic. It took me a full day to get this figured out. I read a few posts that mentioned this technique, but couldn't get it working. Finally found this https://www.codeproject.com/articles/59781/dynamic-loading-of-asp-net-user-controls

and it started to make sense.

Public Class ucChild
    Inherits System.Web.UI.UserControl
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim oucGrandChild As ucGrandChild = TryCast(LoadControl("~/ucGrandChild.ascx"), ucGrandChild)
        oucGrandChild.LabelContent = "first instance"
        Placeholder1.Controls.Add(oucGrandChild)
 
 
        Dim oucGrandChild2 As ucGrandChild = TryCast(LoadControl("~/ucGrandChild.ascx"), ucGrandChild)
        oucGrandChild2.LabelContent = "second instance"
        Placeholder1.Controls.Add(oucGrandChild2)
    End Sub
 
End Class

ucGrandChild.ascx

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="ucGrandChild.ascx.vb" Inherits="test.ucGrandChild" %>
Grandchild content<br />
<asp:Label runat="server" ID="lblGrandchild"></asp:Label><br />
end grandchild content<br />

ucGrandChild.ascx.vb

Public Class ucGrandChild
    Inherits System.Web.UI.UserControl
 
    Public LabelContent As String
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        lblGrandchild.Text = "Grandchild label content:" + LabelContent
    End Sub
 
End Class

 


RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences