How to control elements on a asp.net master page from child page

Lloyd picture Lloyd · Jul 7, 2009 · Viewed 16.6k times · Source

I have a few pages on my asp.net website that I would like to turn off a control on the master page. Is there a way to communicate with the master page from a child page?

Answer

Ahmad Mageed picture Ahmad Mageed · Jul 7, 2009

Easiest way is to setup a property on your master page that handles the on/off functionality when called. Then in your child page set the MasterType directive to get a strongly typed reference to your master page to bypass the need to cast.

Your child page would have:

<%@ MasterType VirtualPath="~/Site1.Master" %>

And to call the property of the master page:

Master.MyLabel = false; // or true

So on your master you could have:

public bool MyLabel
{
    get
    {
        return masterLabel.Enabled;
    }
    set
    {
        masterLabel.Enabled = value;
    }
}