How to add user control dynamically into another usercontrol

Platform: ASP.NET| Category: User Controls

Here is an example of adding one UserControl into another:


uc1.ascx: 
<asp:Label runat='server' text='uc1' ID='Label1' />
<asp:Panel runat='server' id='p1' >Panel UC1</asp:Panel> 

uc2.ascx: 
<br><asp:Label runat='server' text='uc2' ID='Label1' />

VB.NET


Dim uc1 As UserControl = CType(LoadControl('uc1.ascx'), UserControl)
Controls.Add(uc1)
Dim uc2 As Control = uc1.LoadControl('uc2.ascx')
Dim p1 As Control = uc1.FindControl('p1')
p1.Controls.Add(uc2)

C#


UserControl uc1    = (UserControl)LoadControl('uc1.ascx');
Controls.Add(uc1);
Control uc2    = uc1.LoadControl('uc2.ascx');
Control p1    = uc1.FindControl('p1');
p1.Controls.Add(uc2);

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.