ASP.NET master page
When we want the same layout for different web pages in a application, then we can use the asp.net master page. Instead of creating the same layout in all pages we can simple add an master page in the application and create the layout in the master page. So that the layout will be applied to all the content pages (web pages that uses the master page).
Advantages of master page:
- Reduces the re-work, i.e. common layout that we give in master page will applied to all the web pages, no need to write it in individual pages.
- Easy maintenance, i.e. Any changes need to be done in the layout if we update in the master page automatically applied to all the web pages
- Common controls placed in master page will be applicable to all the pages. ex: menu bar, search, ..
Sample master page:
asp:ContentPlaceHolder between head tag
asp:ContentPlaceHolder between body tag
The content pages (web pages that are using the master page) will be rendered as child controls for the asp:ContentPlaceHolder which is written in between body tag. i.e. After the common layout created, the individual page controls design will be rendered under ContentPlaceHolder mentioned in body tag.
Sample content Page:
Applying master page:
We can apply master page in 3 levels.
- Page Level :
In page directive of content page we can define the master page
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" %>
- Application level :
In web.config file by setting the masterPageFile attribute of the pages element
- Folder level :
Add web.config file In Folder level and set the masterPageFile attribute of the pages element