从子用户控件访问基本用户控件属性

本文关键字:用户 控件 属性 访问 | 更新日期: 2023-09-27 18:16:10

我的asp.net应用程序有一个从其他用户控件继承的自定义基本用户控件。此自定义基本用户控件有三个属性已被公开。加载用户控件时,自定义基本用户控件属性为空。我在想我到底做错了什么。有人能帮忙弄清楚我错过了什么步骤吗?

自定义基用户控件从父页面加载代码:

    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。

另外,您可能需要重构基类/自定义类以避免事件执行顺序混淆,因为加载事件将在基类和自定义类中同时触发。

每当我们有这种类型的结构时,我们总是在基类中实现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事件中,以便它们在标准页面逻辑中正确工作。

你试过了吗?

[PropertyName) ?

如果您在派生类中有一个新的关键字或重写,并且只有基类中的值,则可能是罪魁祸首。