Using global objects in your page with a Master Page

MasterPages are very useful to set the site structure like a header and footer, but, they can also help managing authorization or global issues of the site just declaring the needed variables on it and using them, if necessary, from the content pages or user controls. Let’s see this with an example, imagine we need to add a site navigator object for showing dynamic menus, a navigator and other issues.

First of all, we need to declare that variable as a public property:

public MyLibrary.Navigator Navi {get; set;}

We need to initialize the object (because we are using an object in this example) before accesing it or we’ll find a null reference. If we do that in the Page_Load, we will find that the Page.Page_Load loads before the MasterPage.Page_Load giving us a null reference in case we access it from the Page_Load at the child Page. So, we need to initialize it in the MasterPage.Page_Init to avoid problems:

        protected void Page_Init(object sender, EventArgs e) {
            Navi = new MyLibrary.Navigator();
        }

Accessing the object from a Page

Now, we can access the object from the page without a problem. Let’s see how:

    protected void Page_Load(object sender, EventArgs e) {
        if (!Page.IsPostBack) {
            ((MyProject.SiteMaster)this.Master).Navi.LoadOrSomethingElse();
        }
    }

When we access the Page.Master property it gives us a generic MasterPage, so, if we want to access our object we will have to cast that generic MasterPage to our specific MasterPage. In this example, our MasterPage was inside a namespace called “MyProject” and our MasterPage class was called “SiteMaster”.

Once we have got access to the MasterPage and its properties we can now access our global object and use it calling its methods or properties.

Remember: You need to cast the MasterPage to be able to access your specific MasterPage properties.

Warning: For doing this you need to reference the Master Page from the Page, this is done by default if you have set the Page “MasterPage” property to the needed MasterPage, if not, you will have to add a manual reference (note: I’ve not tried it).

Accessing the MasterPage from a Web User Control

Now that we have accessed a MasterPage from a Page, and that lets us access a “global” object or similar, we may need to access it from a Web User Control too. Imagine… what if we make a Menu Control and place it on the page making it totally independent? It would be able to get the User object from the Master Page and show only the options avaliable for this user, without having to make another object and requesting again to the DB.

This time, since the Web User Control isn’t going to have a reference to the MasterPage in one of its properties we have to access it through its Page property like this:

    protected void Page_PreRender(object sender, EventArgs e) {
        user = ((MyProjectNamespace.SiteMasterName)this.Page.Master).User;
        // Do the rest of the stuff
    }

But WebUserControls can be placed inside other WebUsercontrols or inside other pages or even in the MasterPage diretly. In that case, you may find it difficult to get the MasterPage. I made this method to help in that scenario:

        protected void Page_Load(object sender, EventArgs e) {
            User= getMasterPG(this).User;
        }

        private MyProjectNamespace.SiteMasterName getMasterPG(Control ctrl) {
            if (ctrl.Parent != null) {
                if (ctrl.Parent is MyProjectNamespace.SiteMasterName) {
                    return (MyProjectNamespace.SiteMasterName)ctrl.Parent;
                } else {
                    return getMasterPG(ctrl.Parent);
                }
            } else {
                return null;
            }
        }

One thought on “Using global objects in your page with a Master Page

Leave a Reply

Close Bitnami banner
Bitnami