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″
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

Unknown's avatar

Tim Clark

Experienced Business Owner, Chief Information Officer, Vice President, Chief Software Architect, Application Architect, Project Manager, Software Developer, Senior Web Developer, Graphic Designer & 3D Modeler, University Instructor, University Program Chair, Academic Director. Specialties: Ruby, Ruby on Rails, JavaScript, JQuery, AJAX, Node.js, React.js, Angular.js, MySQL, PostgreSQL, MongoDB, SQL Server, Responsive Design, HTML5, XHTML, CSS3, C#, ASP.net, Project Management, System Design/Architecture, Web Design, Web Development, Adobe CS6 (Photoshop, Illustrator)

Leave a comment