Doiremik
Doiremik

Reputation: 265

Accessing different Masterpages from Base Class

I have an application that uses 2 master pages. One for main pages and one for Popup pages. These both inherit from Masterpages

public partial class MainMasterPage : System.Web.UI.MasterPage
{

    UserBO objUser = null;

    public UserBO GetCurrentUser()
    {
        UserBO userBO = new UserBO();
     .....
     .....  
        return userBO;
    }
 }


public partial class PopupMasterPage : System.Web.UI.MasterPage
{

    UserBO objUser = null;

    public UserBO GetCurrentUser()
    {
        UserBO userBO = new UserBO();
     .....
     .....  
        return userBO;
    }
 }

So far so good. So my content pages all Inherit from a base class. The base class has a method that calls the GetCurrentUser from the Base class.

public class BasePage : Page
{
//..... 
    protected UserBO GetCurrentUserFromMasterPage()
    {
        this.Master
        Elsa.MasterPages.MainMasterPage master = (Elsa.MasterPages.MainMasterPage )this.Master;
        return master.GetCurrentUser();
    }
}

So here you can see that the base page casts the MasterPage and then calls GetCurrentUser.

Just for background... The masterpages get current user logged into the system and then draws itself using the info. If the user is in the session it gets it otherwise it loads from the database. I dont want the content pages to do the same so I wanted the base page to always get the current user for the content page from the master.

However my problem is, that because there is 2 master pages and all web pages are derived from Base page.. I need to be able to cast to the correct master.

public partial class MyMainPage : Elsa.Pages.BasePage
{
    private long _userId = -1;

public partial class MyPopupPage : Elsa.Pages.BasePage
{
    private long _userId = -1;

If I put in the MasterType directive I can call the method in the content page for the correct Master. But I dont want to call it from the content as its common method so I need it in the base.

So does anyone know how to handle this. I was thinking on deriving the BasePage again for a PopupBasePage and over writing the GetCurrentUserFromMasterPage() to cast to the popup master. Or do I pass something into the BasePage constructor to tell it what to cast to.

I want to keep the impact to all my web pages to a minium as I have a lot of web pages.

Thanks M

Upvotes: 0

Views: 169

Answers (3)

Derek
Derek

Reputation: 8630

Do this :-

    public class MasterPageBase : MasterPage
    {

    public PageBase PageBase { get { return (PageBase)this.Page; } }

    }

    public class PageBase : Page
    {
      // Do your Extensions Here.. 
    }

All Pages there after Inherit from PageBase.

Upvotes: 0

Henk Holterman
Henk Holterman

Reputation: 273179

You can insert an extra MasterPage as the base class for your 2 current ones:

public partial class SiteMasterPage : System.Web.UI.MasterPage
{
   ....
   // GetCurrentUser
}

public partial class MainMasterPage : SiteMasterPage
{
   ....
}

public partial class PopupMasterPage : SiteMasterPage
{
}

This will allow you to implement other common features and markup (include of CSS files) in one place as well.

Upvotes: 2

Abhishek Jain
Abhishek Jain

Reputation: 2607

Does it make sense for you

using Reflection:

Master.GetType().GetMethod("GetCurrentUser").Invoke();

Upvotes: 0

Related Questions