- NET master pages allow you to create a consistent layout for the pages in your application.
- A single master pages defines the look and feels and standard behavior that you want for all of the pages (or a group of pages) in your application.
- You can then create individual content pages that contain the content you want to display.
- When users request the content pages, they merge with the master pages to produce output that combines the layout of the master page with the content from the content page.
Types of Master pages
Simple Master Page
Nested Master Page
- Here you can merge the master pages and content page.
- Simply, Main master page à another master pages for some selected pages that is called nested master page.
- It is used to give the common and distinct look from a master pages for some special module.
How Master Pages Work
Master pages actually consist of two pieces, the master page itself and one or more content pages.
- A master page is an ASP.NET file with the extension .master (for example, MySite.master) with a predefined layout that can include static text, HTML elements, and server controls.
- The master pages are identified by a special @ Master directive that replaces the @ Page directive that is used for ordinary .aspx pages.
Replaceable Content Placeholders
- In addition to static text and controls that will appear on all pages, the master pages also includes one or more ContentPlaceHolder controls.
- These placeholder controls define regions where replaceable content will appear.
- In turn, the replaceable content defined in content pages. After you have defined the ContentPlaceHolder controls, a master pages might look like the following.
- You define the content for the master page’s placeholder controls by creating individual content pages, which are ASP.NET pages (.aspx files and, optionally, code-behind files) that are bound to a specific master page.
- The binding established on the content page’s @ Page directive by including a MasterPageFile attribute that points to the master page to use.
- After creating Content controls, you add text and controls to them.
- In a content page, anything that not inside the Content controls (except script blocks for server code) results in an error. You can perform any tasks in a content page that you do in an ASP.NET page. For example, you can generate content for a Content control using server controls and database queries or other dynamic mechanisms.
- A content page might look like the following.
Advantages of Master Pages
They allow you to centralize the common functionality of your pages so that you can make updates in just one place.
They make it easy to create one set of controls and code and apply the results to a set of pages. For example, you can use controls on the master pages to create a menu that applies to all pages.
Moreover, They give you fine-grained control over the layout of the final page by allowing you to control how the placeholder controls rendered.
They provide an object model that allows you to customize the master page from individual content pages