For controls, events are handled locally—a control in a content page raises an event in the content page, and a control in the master page raises an event in the master page. Controls events are not sent from the content page to the master page. Similarly, you cannot handle an event from a master page control in a content page.
In some cases, the same event is raised in both the content and the master page. For example, both pages raise Init and Load events. The general rule for how events are raised is that the initialization events are raised from the innermost control to the outermost one, and all other events are raised from the outermost control to the innermost one. It is helpful to remember that the master page is merged into the content page and treated as a control in the content page, which you can access via the Page's Master property.
The following is the order in which events occur when the master page is merged with a content page:
-
Master page controls Init event.
-
Content controls Init event.
-
Master page Init event.
-
Content page Init event.
-
Content page Load event.
-
Master page Load event.
-
Content controls Load event.
-
Content page PreRender event.
-
Master page PreRender event.
-
Master page controls PreRender event.
-
Content controls PreRender event.
So, to answer your question, you can access the controls on the MasterPage from the child page safely once the MasterPage Load event has fired.
You can write code in content pages that references properties, methods, and controls in the master page, with some restrictions. The rule for properties and methods is that you can reference them if they are declared as public members of the master page. This includes public properties and public methods. You can reference controls on the master page independently of referencing public members.
To do this:
1. Add a Mastertype:
<%@ MasterType virtualpath="~/Masters/Master1.master" %>
2.Write code that uses the master page's public member as a member of the Master property, as in this example, which assigns the value of a public property named CompanyName from the master page to a text box on the content page:
CompanyName.Text = Master.CompanyName;