Asp.net 是否可以将UserControl添加到复合控件中?
我正在尝试构建一个灵活的CompositeControl。根据它的一些参数,我希望CompositeControl在其CreateChildControls方法中加载不同的用户控件。具体的用户控件在设计时不知道 举个简单的例子,我尝试使用“硬编码”用户控件,但失败了:Asp.net 是否可以将UserControl添加到复合控件中?,asp.net,composite-controls,Asp.net,Composite Controls,我正在尝试构建一个灵活的CompositeControl。根据它的一些参数,我希望CompositeControl在其CreateChildControls方法中加载不同的用户控件。具体的用户控件在设计时不知道 举个简单的例子,我尝试使用“硬编码”用户控件,但失败了: protected override void CreateChildControls() { Control UserControlControl = Page.LoadContr
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);
}
有可能实现我所追求的吗
谢谢请尝试以下操作:
protected override void CreateChildControls()
{
Control UserControlControl = Page.LoadControl("~/path/to/control.ascx");
Controls.Add(UserControlControl);
}
失败的信息是什么?是的,就是这样。这样就可以了,但我仍然很好奇如何使用另一个重载方法,将类型作为参数。