gpt4 book ai didi

c# - 通过 LoadControl(Type, object[]) 重载以编程方式加载用户控件时,子控件为空

转载 作者:太空狗 更新时间:2023-10-29 18:14:27 25 4
gpt4 key购买 nike

我正在像这样以编程方式加载用户控件:

        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);
}

使用这个构造函数:

        public CatalogoGenerico(Type type):this()
{
_db = new DataClasses1DataContext();
_type = type;

}

我遇到的问题是我的用户控件中的所有控件都是空的,我还需要做些什么来加载子控件吗?

最佳答案

这是设计使然。 .ascx 文件实际上继承自代码隐藏类,因此.ascx 是代码隐藏类的派生类型。

这意味着当您使用 LoadControl(Type, object[]) 方法加载父代码隐藏类时,它实例化在代码隐藏中定义的父类,而不是派生类包含子控件的 .ascx 类。

如果您使用 Page.LoadControl(string) 重载,它将如您所愿地工作,因为它可以正确地找到模板、找到已编译的 .ascx 类并加载它。

另一种方法是使用代码隐藏而不是标记来实例化 .ascx 文件中的所有控件。

关于c# - 通过 LoadControl(Type, object[]) 重载以编程方式加载用户控件时,子控件为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2241394/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com