【发布时间】:2009-10-08 13:47:51
【问题描述】:
我有一个动态加载其他用户控件的用户控件。问题是在回发时我的控件不存在。现在我的印象是我必须重新初始化它们。
现在要执行此操作,因为它们是 UserControls 并使用 ascx 文件,因此 UserControl 的任何控件都没有在空构造函数中初始化。
问题:此时如何加载UserControl的ascx文件?
目前我正在尝试这样做:
for (int i = 0; i < count; i++)
{
Control ctrl;
if(ctrlCollectionType.Rows[i]["Type"] is UserControl)
ctrl = LoadControl((string)ctrlCollectionType.Rows[i]["Path"]);
else
this.LoadControl(ctrlCollectionType[i]["Type"], null);
ctrl.ID = i;
pnlContent.Controls.Add(ctrl);
}
其中 ctrlCollectionType 是 userControl 的类型。
编辑:根据 Joel Coehoorn 的意见解决。
【问题讨论】: