Embedding user controls into html page is easy but what about embedding user controls dynamically from code behind c#? Any help/suggestions would be greatly appreciated.
Use LoadControl
to load and create a control dynamically. Then you can add it to the page's control-collection.
UcType myControl = (UcType) LoadControl("UcType.ascx");
this.SomeContainerContainerControl.Controls.Add(myControl);
where UcType
is the type of your UserControl
and SomeContainerContainerControl
is the control you want to add this uc like a Panel
.
If you want to add it on top of the page simply use this.Controls.Add(myControl)
.
You should add it in Page_Init
or Page_Load
(at the latest). You have to recreate it on every postback as every other dynamic control with the same ID as before (if any).