professional web development and design
 

Dynamically Loading Controls in ASP.NET


Below is an example on how to dynamically create a span in a page, and load a user control into the span:
// create the serverside form (remember there can only be one per .aspx page...)
HtmlForm form = new HtmlForm();
// this will translate into a <span> when rendered
System.Web.UI.HtmlControls.HtmlGenericControl contentPanel = null;
// the controlfile is the filename of the file we wish to load
// (for example, "ucAccountList.ascx")
System.Web.UI.Control control = Page.LoadControl(Request.ApplicationPath; + "/" + controlFile);
// create the current Panel
contentPanel
	= new System.Web.UI.HtmlControls.HtmlGenericControl();
// assign it an idea based on the regionName in the DB
contentPanel.ID = regionName;
// this will also associate it with a style 
// className is a css class...
contentPanel.CssClass = className;




back to development articles back next ASP.NET dynamic user controls
Valid HTML 4.01