2011-12-15 36 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++) 
    { 
    } 
相關問題