Nugs
Nugs

Reputation: 5793

Loading UserControl programmatically using web.config control references

I'm pretty familiar with loading user controls programatically, however i would like to use the references set in the web.config rather than the <%@ Reference %> declaration on the page.

<pages>
  <controls>
    <add tagPrefix="uc" tagName="Menu" src="~/App_Controls/MainMenu.ascx" />...

How do i programatically load this control on my page using the web.config reference, not a path or register declaration.

        Control uc = (Control)Page.LoadControl("~/usercontrol/WebUserControl1.ascx");
        plhStatCounts.Controls.Add(uc);

Thanks

Upvotes: 1

Views: 469

Answers (1)

competent_tech
competent_tech

Reputation: 44971

You can load the PagesSection from web.config, then access its Controls collection.

For example example:

    // Open the config
    Configuration webConfig = WebConfigurationManager.OpenWebConfiguration("");

    // Retrieve the section
    PagesSection pages = (PagesSection)webConfig.GetSection("system.web/pages");

    // Find the control you are interested in, then load it
    for (int i = 0; i < pages.Controls.Count; i++)
    {
    }

Upvotes: 1

Related Questions