Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 988
Lloyd Avatar asked Jul 07 '09 14:07

Lloyd


People also ask

Can you access controls on the master page without using FindControl () method?

You can access the Masterpage as a property on your current page. However, the controls on your master page are protected so you can't access them directly. But you can access them by using FindControl(string name) .

How do I access the master page label in the content page?

we create the instance of master page button click event before content page raises its page_load event. To access the master page controls we need to use the Master key word then call the Property method of control. As we use Master. EnterNameTextBox() to access the master page EnterName textbox and Master.


1 Answers

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;
    }
}
like image 62
Ahmad Mageed Avatar answered Oct 17 '22 18:10

Ahmad Mageed