You can writeCodeTo reference properties, methods, and controls on the master page. The rules for attributes and methods are: if they are declared as public members on the master page, they can be referenced. This includes public attributes and public methods. When referencing controls on the master page, there is no such restriction that only public members can be referenced.
Reference public members on the master page
1. Add the @ mastertype command to the content page. In this command, set the virtualpath attribute to the master page location, as shown in the following example: <% @ mastertype virtualpath = "~ /Masters/master1.master "%> This command causes the master attribute of the content page to be strongly typed.
2. write code and use the public members on the master page as a member of the master attribute. In this example, assign the value of the public property of the master page name companyName to a text box on the Content Page.
Reference controls on the master page
Use the findcontrol method to use the return value of the master attribute as the naming container.
The following code example demonstrates how to use the findcontrol method to obtain references to two controls on the master page (one Textbox Control and one label control ). Because the Textbox Control is inside the contentplaceholder control, you must first obtain the reference to contentplaceholder, and then use its findcontrol method to locate the Textbox Control.
Reference the content of the dashboard page
Void Page_load ()
{
// Gets a reference to a Textbox Control inside
// A contentplaceholder
Contentplaceholder mpcontentplaceholder;
Textbox mptextbox;
Mpcontentplaceholder =
(Contentplaceholder) master. findcontrol ( " Contentplaceholder1 " );
If (Mpcontentplaceholder ! = Null )
{
Mptextbox =
(Textbox) mpcontentplaceholder. findcontrol ( " Textbox1 " );
If (Mptextbox ! = Null )
{
Mptextbox. Text= "Textbox found!";
}
}
// Gets a reference to a label control that not in
// A contentplaceholder
Label mplabel = (Label) master. findcontrol ( " Masterpagelabel " );
If (Mplabel ! = Null )
{
Label1.text= "Master page label =" +Mplabel. text;
}
}