Master Page and Content Page


Using Master page we can create a consistent layout for the pages in our application.  The look and feel of all the pages in our application can be defined using a single master page. Individual content pages are created which contain the content which we want to display.

Master Page 

Extension of Master page is .master. The master page is identified by a special @Master directive that replaces the @Page that is used for ordinary .aspx . The @Master directive can contain most of the same directives that an @Page directive can. In addition to the @Master directive, the master page also contains all the top-level HTML elements for a page, such as <html>, <head> and <form>. On the master page we can include one or moreContentPlaceHolder controls that define regions where replaceable content will appear. We can also use HTML and ASP.NET elements as part of our master page.

Content Page

Content for the placeholders can be defined by creating individual content pages(.aspx). Such Content Pages are bound to the specific master page.The binding of the content page with the master page is possible, using @Page directive and MasterPageFile attribute. Extension of Content page is .aspx

Run-Time Behavior of Master Pages

At run time, master pages are handled in the following sequence:

1. Content Page is requested in the browser.
2. ASP.NET processes the request.
    a. ASP.NET fetches the page.
    b. @Page directive is read.
    c. If the directive references a master page, the master page is read as well.
    d. ASP.NET fetches the master page.
    e. The master page with the updated content is merged.
    f. At last the resulting merged page is rendered to the browser.

The process is described in the following diagram:

How to create a Master Page

1. Right Click the web site present on the Solution Explorer.
2. Add New Item.
3. Click Master Page.
4. In the name box, by default MasterPage.master appears. But we can change its name also. Only extension should be .master.
5. Select C# language in the Language list.
6. Now click Add.

Designing the Master Page

When a new master page is created,  two ContentPlaceHolder controls are available in it. We can add additional ContentPlaceHolder controls on the master page. This control provides a location where content from referencing pages will be merged at run time.

Creating a Content Page

1. Right Click the website present in the Solution Explorer.
2. Add New Item.
3. Choose Web Form.
4. Enter the name in the Name box.
5. Check the Select Master Page check box.
6. Click Add Button.

7. Select the master page.
8. Click at OK Button.