【问题标题】:Loading UserControl programmatically using web.config control references使用 web.config 控件引用以编程方式加载 UserControl
【发布时间】:2011-12-15 17:39:16
【问题描述】:

我非常熟悉以编程方式加载用户控件,但是我想使用 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);

谢谢

【问题讨论】:

  • 您提到了以编程方式添加用户控件,但编程用户控件不需要使用@Reference 部分或网络配置,所以您打算静态定义用户控件吗?

标签: c# asp.net


【解决方案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++)
    {
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-25
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 2011-01-24
    • 1970-01-01
    • 1970-01-01
    • 2015-09-10
    相关资源
    最近更新 更多