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