1

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

1
  • You mention programmatically adding a user control, but programmatic user controls do not need to make use of the @Reference section or the web config, so are you planning on defining the user controls statically? Commented Dec 15, 2011 at 19:07

1 Answer 1

1

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++)
    {
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.