0

我正在尝试构建一个灵活的 CompositeControl。根据它的一些参数,我希望我的 CompositeControl 在它的 CreateChildControls 方法中加载不同的用户控件。在设计时不知道确切的用户控件。

举个简单的例子,我尝试了一个“硬编码”的 UserControl,但它失败了:

protected override void CreateChildControls()
    {            
        Control UserControlControl = Page.LoadControl(typeof(MyUserControl), null);
        Controls.Add(UserControlControl);
        Label RegularControl = new Label();
        RegularControl.Text = "This gets displayed";
        Controls.Add(RegularControl);
    }

是否有可能达到我正在寻找的东西?

谢谢

4

1 回答 1

2

尝试以下操作:

protected override void CreateChildControls()
{            
            Control UserControlControl = Page.LoadControl("~/path/to/control.ascx");
            Controls.Add(UserControlControl);
}
于 2009-06-25T23:03:41.373 回答