[英]Accessing base user control properties from child user control
我的asp.net应用程序具有从其他用户控件继承的自定义基本用户控件。 此自定义基本用户控件具有三个公开的属性。 加载用户控件后,自定义基本用户控件属性为null。 我试图弄清楚我在做什么错。 有人可以帮忙弄清楚我缺少什么步骤吗?
来自父页面的自定义基本用户控件加载代码:
private void Render_Modules()
{
foreach (OnlineSystemPageCustom.OnlineSystemPageHdr.OnlineSystemPageModule item in custompage.Header.Modules)
{
if (item.ModuleCustomOrder != 99)
{
webonlinecustombase ctl = (webonlinecustombase)Page.LoadControl("../IPAM_Controls/webtemplatecontrols/webonlinecustombase.ascx");
ctl.Event = Event;
ctl.custompage = custompage;
ctl.custommodule = item;
this.eventprogrammodules.Controls.Add(ctl);
}
}
}
自定义基本用户控制代码
public partial class webonlinecustombase : System.Web.UI.UserControl
{
public Event Event { get; set; }
public OnlineSystemPageCustom custompage { get; set; }
public OnlineSystemPageCustom.OnlineSystemPageHdr.OnlineSystemPageModule custommodule { get; set; }
public void Page_Load(object sender, EventArgs e)
{
string typeName = custommodule.ModuleInternetFile;
inpagelink.HRef = "#" + custommodule.ModuleName.Replace(" ", "").Replace("/", "");
modtitle.InnerText = custommodule.ModuleName;
Type child = Type.GetType(typeName);
UserControl ctl = (UserControl)Page.LoadControl(child, null);
if (ctl != null)
{
this.modsection.Controls.Add(ctl);
}
}
}
继承基本用户控件的用户控件的示例代码
public partial class eventscientificoverview : webonlinecustombase
{
protected void Page_Load(object sender, EventArgs e)
{
if (custommodule.ModuleDefaultVerbiage != null && custommodule.ModuleDefaultVerbiage != "") { this.Load_Verbiage(false); }
else if (custommodule.ModuleCustomVerbiage != null && custommodule.ModuleCustomVerbiage != "") { this.Load_Verbiage(true); }
}
protected void Load_Verbiage(bool usecustom)
{
if (usecustom) { this.scientificoverviewverbiage.InnerHtml = custommodule.ModuleCustomVerbiage; }
else { this.scientificoverviewverbiage.InnerHtml = custommodule.ModuleDefaultVerbiage; }
}
}
您必须在父页面的init事件中调用Render_Modules。
另外,您可能希望重组基类/自定义类,以避免事件执行顺序混乱,因为在基类和自定义类中都会触发load事件。
每当我们拥有这种类型的结构时,我们总是在基类中实现OnLoad方法,以使继承者重写。 这样,我们可以精确控制何时在继承程序中执行Load逻辑。
更新了其他信息
这是有关如何处理基类和子类中的装入事件的一些其他信息。
在webonlinecustombase中,添加以下内容:
protected virtual void OnPageLoad() {
}
然后修改页面加载事件以在适当的时间调用此新方法:
public void Page_Load(object sender, EventArgs e)
{
string typeName = custommodule.ModuleInternetFile;
inpagelink.HRef = "#" + custommodule.ModuleName.Replace(" ", "").Replace("/", "");
modtitle.InnerText = custommodule.ModuleName;
Type child = Type.GetType(typeName);
UserControl ctl = (UserControl)Page.LoadControl(child, null);
if (ctl != null)
{
this.modsection.Controls.Add(ctl);
}
// Now let the inheritors execute their code
OnPageLoad();
}
然后,在您继承的类中,更改:
protected void Page_Load(object sender, EventArgs e)
至
protected override void OnPageLoad()
在查看此代码时,我发现您还在webonlinecustombase中动态加载控件。 您需要将控件的加载移到init事件中,以使它们在标准页面逻辑中正常工作。
您尝试使用base。[PropertyName]吗?
如果您在派生类中使用新的关键字或重写,并且仅基类中的值可能是罪魁祸首。 这以前发生在我身上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.