繁体   English   中英

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

[英]Loading UserControl programmatically using web.config control references

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

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

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

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

谢谢

您可以从web.config加载PagesSection ,然后访问其Controls集合。

例如:

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