2011-12-15 85 views
1

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

<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); 

感谢

+0

你提到编程添加用户的控制,但程序的用户控件不需要利用@Reference部分或web配置的,所以你在定义用户计划控制静态? – 2011-12-15 19:07:04

回答

1

您可以加载从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++) 
    { 
    } 
相关问题