我正在以编程方式加载用户控件,如下所示:
protected void Page_Load(object sender, EventArgs e)
{
// LinqDataSource1.TableName = string.Format("{0}s", _table.Context.Mapping.GetMetaType(_type).Name);
_control = Page.LoadControl(typeof(CatalogoGenerico), new object[] { typeof(CTG_ENT_ENTIDAD) }) as CatalogoGenerico;
PlaceHolder1.Controls.Add(_control);
}
Run Code Online (Sandbox Code Playgroud)
使用此构造函数:
public CatalogoGenerico(Type type):this()
{
_db = new DataClasses1DataContext();
_type = type;
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是我的用户控件中的所有控件都为null,是否还需要加载子控件?
wom*_*omp 17
这是设计的..ascx文件实际上是从代码隐藏类继承的,因此.ascx是代码隐藏类的派生类型.
这意味着当您使用该LoadControl(Type, object[])方法加载父代码隐藏类时,它将实例化代码隐藏中定义的父类,而不是包含子控件的派生.ascx类.
如果你使用Page.LoadControl(string)重载,它将按你的意愿工作,因为它可以正确地找到模板,找到已编译的.ascx类,并加载它.
另一种方法是使用代码隐藏而不是标记来实例化.ascx文件中的所有控件.