简体   繁体   中英

ASP.NET C# object in Master Page to be visible to all other pages that are based on that Master Page

I need to be able to make an object that is instantiated in a ASP.NET Master Page to be visible to all of the pages in the application that are based on that Master page. At the moment I'm defining the object here:

public partial class Control : System.Web.UI.MasterPage
{
    public User TheUser;

    protected void Page_Load(object sender, EventArgs e)

... but I think that's where I'm going wrong. Can anyone help?

You'd be better off creating a base page (that inherits from System.Web.UI.Page ) that all your pages inherit from, add your TheUser object to the base page.

Please note, pages do not inherit their master pages.

Are you trying to create a control that inherits from a MasterPage? What you really want to do is create a MasterPage and add a property to it:

public partial class MyMasterPage: System.Web.UI.MasterPage
{
    public string MyProperty {get;set;}
}

Now, from a Page that uses that MasterPage, you can just reference the MasterPage and access its members directly:

protected void Page_Load(object sender, EventArgs e)
{
    MyMasterPage m = Master as MyMasterPage;
    string masterProperty = m.MyProperty;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM