简体   繁体   English

使用web.config控件引用以编程方式加载UserControl

[英]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. 我非常熟悉以编程方式加载用户控件,但是我想使用在web.config中设置的引用,而不是页面上的<%@ Reference %>声明。

<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. 如何使用web.config参考而不是路径或寄存器声明以编程方式将此控件加载到我的页面上。

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

Thanks 谢谢

You can load the PagesSection from web.config, then access its Controls collection. 您可以从web.config加载PagesSection ,然后访问其Controls集合。

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++)
    {
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM