C# Dynamic Control Loading in ASP.net

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″

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;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s