MasterPage in ASP
MasterPage in ASP
NET
It allows developers to create a consistent look and feel across multiple pages by defining
common elements such as headers, footers, and navigation menus in a single location. Child
pages can then inherit the layout and visual elements of the master page, while still being able
to define their own unique content. This makes it easier to maintain and update the overall
design of a website, as changes only need to be made in one place.
• Master pages provide templates for other pages on your web site.
• Master pages allow you to create a consistent look and behavior for all the pages (or
group of pages) in your web application.
• A master page provides a template for other pages, with shared layout and functionality.
The master page defines placeholders for the content, which can be overridden by content
pages. The output result is a combination of the master page and the content page.
• The content pages contain the content you want to display.
• When users request the content page, ASP.NET merges the pages to produce output that
combines the layout of the master page with the content of the content page.
Master Pages can also improve the performance and security of a website by caching
common elements on the browser and separating them from the unique content. It can also
enhance SEO by providing a consistent layout and navigational links.
Benefits of MasterPage:
There are several benefits of using a Master Page in ASP.NET:
1. Consistent layout: A Master Page allows for a consistent layout across all pages in a
website, which helps to improve the user experience.
2. Code reuse: By defining a common layout in a Master Page, developers can reduce
the amount of code that needs to be written and maintained, resulting in less time and
effort required to create and update a website.
3. Easy maintenance: By centralizing the layout on a Master Page, it is much easier to
make updates or changes to the overall look and feel of a website.
4. Improved organization: Master Pages can help to organize the codebase of a
website, making it easier for developers to understand the structure and layout of the
site.
5. Enhance security: By using Master Pages, the common elements like the header,
footer, and menus are separate and can be easily secured without affecting the other
pages.
6. Better SEO: With a consistent layout and navigational links, Master Pages can help
improve the SEO of the website.
7. Better performance: By using Master Pages, the common elements are cached on
the browser which improves the performance of the website.
Terminologies used in Masterpage:
Below are several key terminologies used in Master Pages in ASP.NET:
1. Master Page: A file that defines the layout and common elements of a website, such
as a header, footer, and navigation menu.
2. Content Page: A file that "inherits" the layout defined in a Master Page and contains
unique content for a specific page.
3. ContentPlaceHolder: A control that marks a region in a Master Page where content
from a Content Page will be displayed.
4. MasterType: A directive that allows a Content Page to access properties and methods
of Master Page.
5. Content: A control that defines the content that will be displayed in a
ContentPlaceHolder.
6. MasterPageFile: A property that specifies Master Page that a Content Page will
inherit from.
7. Page directive: A control that specifies Master Page that a Content Page will inherit
from and other properties of the page.
8. Event handling: Master Pages can raise events and the content pages can handle
those events through event bubbling.
9. Nested Master Pages: Master Pages can also inherit from other Master Pages.
10. Data binding: Master Pages can access data from the content pages and bind it to its
controls.
<html>
<body>
<h1>Standard Header From Masterpage</h1>
<asp:ContentPlaceHolder id="CPH1" runat="server">
</asp:ContentPlaceHolder>
</body>
</html>
The master page above is a normal HTML page designed as a template for other pages.
The @ Master directive defines it as a master page.
The master page contains a placeholder tag <asp:ContentPlaceHolder> for individual content.
The id="CPH1" attribute identifies the placeholder, allowing many placeholders in the same
master page.
This master page was saved with the name "master1.master".
Note: The master page can also contain code, allowing dynamic content.
Content Page Example
<%@ Page MasterPageFile="master1.master" %>
The content page above is one of the individual content pages of the web.
The content page contains a content tag <asp:Content> with a reference to the master page
(ContentPlaceHolderId="CPH1").
When the user requests this page, ASP.NET merges the content page with the master page.
Note: The content text must be inside the <asp:Content> tag. No content is allowed outside the
tag.
The content page above demonstrates how .NET controls can be inserted into the content page
just like an into an ordinary page.