如何从主模板访问通用页面类属性

时间:2014-07-11 23:08:07

标签: c# asp.net generics master-pages

我已经创建了一个通用页面类,我的几个页面都来自这个类。我使用这个类来读取当前请求并使用EF 6.0选择对象列表。

public class EOPKeuzelijst<TEntity> 
    : EntityOverviewPage<TEntity> where TEntity : EntityBase,
    IRR_ProjectId_N,
    IRR_ObjectTypeId_N
{
    public Int32? ProjectId { get; set; }
    public Int32? ObjectTypeId { get; set; }

    // No more info needed..
}

我的问题是我想从附加到它的母版页文件中访问这些属性。

public partial class Keuzelijsten_Template : System.Web.UI.MasterPage
{
    private EOPKeuzelijst<What to do here??> page;
    protected void Page_Load(object sender, EventArgs e)
    {
        this.page = (EOPKeuzelijst<What to do here??>)this.Page;
        this.page.ProjectId

        Response.Write(this.Page.Title);
    }
}

我自己找不到解决方案..有没有人知道如何将this.Page转换为我的通用页面类,或者如何以其他方式访问这些?

提前致谢!

1 个答案:

答案 0 :(得分:1)

您将无法,因为该类在运行时更改,您将无法知道它在编译时是什么。我建议添加一个通用化类将实现的接口,它具有ProjectID getter:

public interface IEOPHasProject { int? ProjectId { get; } }

public class EOPKeuzelijst<TEntity>
    : EntityOverviewPage<TEntity> where TEntity : EntityBase,
    IRR_ProjectId_N,
    IRR_ObjectTypeId_N,
    IEOPHasProject
{
    public Int32? ProjectId { get; set; }
    public Int32? ObjectTypeId { get; set; }

    // No more info needed..
}


public partial class Keuzelijsten_Template : System.Web.UI.MasterPage
{
    private IEOPHasProject page;
    protected void Page_Load(object sender, EventArgs e)
    {
        this.page = (IEOPHasProject)this.Page;
        this.page.ProjectId // will work

        Response.Write(this.Page.Title);
    }
}