Mark Rijsmus
Mark Rijsmus

Reputation: 627

How to access a generic page class property from a master template

I've created a generic page class which a couple of my pages derive from. I use this class to read the current request and select a list of objects using 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..
}

My problem is that i would like to access these properties from the masterpage file attachted to it.

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

I could not find the solution myself.. Does anybody know how to cast the this.Page to my generic page class, or how to access these in another way?

Thanks in advance!

Upvotes: 0

Views: 123

Answers (1)

Jeff Koch
Jeff Koch

Reputation: 366

You won't be able to, since that class changes at runtime you won't be able to know what it is at compile time. I would recommend adding an interface that that genericized class will implement, that has a 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);
    }
}

Upvotes: 1

Related Questions