First register the controls you will be dynamically loading in the .aspx/.ascx files.
With this registration block your dynamically created controls will appear as follows within the document.
<uc1:TemplateDisplay id=”Header1″
runat=”server”></uc1:TemplateDisplay>
To add the controls dynamically use the code below to reference the control through its virtual path, cast it to its correct type, pass it its required parameters or set its properties. Then it is time to add it to a control collection, this can either be the control collection of the page or web user control you are developing or another ASP.net container control such as a PlaceHolder.
TemplateDisplay display = (TemplateDisplay) Page.LoadControl(“~/Library/Controls/TemplateDisplay.ascx”);
display.Template = template;
phTemplateDisplays.Controls.Add(display);
Published by