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?
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) .
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.
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With