Master pages in ASP.NET 2.0 applications are the pages that enable you to provide a consistent look to your web application. In ASP.NET 1.1, to achieve a consistent look across a website, you need to use User controls and place them on each page. The master pages eliminate the need to place the header, footer, or other important sections on each page of your website repeatedly. The master pages are programmable and contain methods, properties, and controls that can be made visible in all other content pages. However, these elements need to be declared with the Public scope in the master pages. You can build your content pages, which have a unique content of their own and then merge the master page with it to provide the page a consistent look. A single website can have more than one master page. To use master page in your content page, you need to reference the master page that you want to use. This article discusses the way that you can use to reference a master page in your content page.

Introduction:

Master pages in ASP.NET 2.0 applications are the pages that enable you to provide a consistent look to your web application. In ASP.NET 1.1, to achieve a consistent look across a website, you need to use User controls and place them on each page. The master pages eliminate the need to place the header, footer, or other important sections on each page of your website repeatedly.

The master pages are programmable and contain methods, properties, and controls that can be made visible in all other content pages. However, these elements need to be declared with the Public scope in the master pages. You can build your content pages, which have a unique content of their own and then merge the master page with it to provide the page a consistent look.

A single website can have more than one master page. To use master page in your content page, you need to reference the master page that you want to use. This article discusses the way that you can use to reference a master page in your content page.

ContentPlaceHolders in Master pages:

The master page is identified by a special @Master directive as opposed to the @ Page directive that is used content pages. To bind a content page to a master page, you need to provide the name of the master file in the MasterPageFile attribute of the page directive of the content page, as shown in the code given below:



Every master page has at least one ContentPlaceHolder that is used to place the controls or content that needs to be merged with the content pages of a web application, as shown in the code below:



On the other hand each content page will also have a Content Server control. The ContentPlaceHolderID attribute in a server control has a special relevance because it decides what content will be bound to which ContentPlaceHolder on the master page. For example the Content1 asp control is bound to the Title ContentPlaceHolder of the master file, in the code given below:



Referencing Master Page Variables:

To understand the referencing of master page through the content pages, we will first create a master file and then a content file. We will then further implement referencing of master page variables through a content page.

Creating a Master Page:

To create a master page, follow the steps given below:

Right click the application in the solution explorer and select Add New Item from the menu that appears. Select Master Page from the Visual Studio installed templates dialog box. Provide the name of the master page in the Name field or leave the default name MasterPage.master. Select Place code in separate file if it is not already selected and then select the Language for your master page. This will generate the code-behind in the language you have selected.

Click Add, as shown in Figure 1:



The master page will be added to your project.

Switch to Design view of the Master page, MasterPage.master (in this example) to design your master page. Alternatively, you can write the code for your master page.
Design your master page by adding a table and setting the background color.
Add a heading to the page “ Welcome to”
Drag 2 label controls and name them as company and time, as shown in the code below:



Double click anywhere on the MasterPage.Master design view or open the MasterPage.master.cs.

Add the following code to specify the values of the labels you have added to the MasterPage.master in the Page_Load event handler.



Creating a Content Page:

We will now create the content page and merge the contents of the master page to the content page. To create a content page that will bind to the master page that you have created, follow the steps given below:

Right click the application in the solution explorer and select Add New Item from the menu that appears.
Select Web Form from the Visual Studio installed templates dialog box.
Select Select Maser Page
Click Add. The Select a Master Page dialog box appears.
Select the master page that you want to associate with this content page and click OK, as shown in Figure 3:



Switch to the design view of the content page, in this case default.aspx.

The design view appears, as shown in Figure

Notice the design view of the content page displays all the contents of the master page. Further notice that the contents of the master page appear dim because they are not editable. However, the content place holder is enabled. You can click the white area of the content place holder and add any controls on the content page that are specific to that page.



Declare the labels that were defined on the master page to use them on the content page, as shown in the code below:

Add more controls specific to the content page if required.



Open the default.aspx.cs file to open the Page_Load event handler.

Add this.MasterPageFile to the labels, to get the reference of the labels declared on the master page. This will bind the Master Page file to the label.

Add the code, as shown below.


You can also refer to any control on the master page by using this.Master.FindControl(string id) method. This method will allow you to take the Id of the control on the master page and get the Control object. You need to then cast to the data type of the control.

Now build and run your application. The content page will display the controls that you have added to the master page, as shown in Figure:



Conclusion:

This article provides a brief overview of master pages, which is a new addition to ASP.NET2. The article further explains the referencing of master page variables through a content page by creating a master page and a content page, adding controls to master page, and then using those controls on the content page.

[Download Sample]