2011-11-30 139 views
0

我的asp.net应用程序具有从其他用户控件继承的自定义基本用户控件。此自定义基本用户控件具有已公开的三个属性。当用户控件加载时,自定义的基本用户控件属性为空。我试图弄清楚我做错了什么。有人可以帮忙找出我失踪的步骤吗?从父页从子用户控件访问基本用户控件属性

定制基本用户控制加载代码:后面的用户控制的

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

示例代码

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

回答

1

您必须在父页面的init事件中调用Render_Modules。

此外,您可能希望重构基类/自定义类以避免事件执行顺序混淆,因为加载事件将在基类和自定义类中触发。当我们有这种类型的结构时,我们总是在基类中实现一个OnLoad方法,以供继承者覆盖。通过这种方式,我们可以准确控制何时在继承器中执行加载逻辑。

带有附加信息的

这里更新是关于如何处理基地和子类加载事件的一些附加信息。

在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事件中才能使它们在标准页面逻辑中正常工作。

+0

好吧,这是所有可以理解的,不太确定的OnLoad方法我明白它,但不知道如何去做。 – mattgcon

+0

@mattgcon:更新了有关如何实施OnLoad和其他发现的信息。 –

+0

我将Render_Modules放置在父页面的Page_Init事件中,但属性仍然在继承基本用户控件的控件中引用null – mattgcon

0

你试过基地吗?[PropertyName]?

如果在派生类中有新的关键字或重写,并且只有基类中的值可能是罪魁祸首。这发生在我以前。

相关问题