What is Master Page in Asp.net and how to use?
In ASP.NET, a master page is a template that provides a consistent layout and structure for the web pages in an application. It allows you to define a common set of controls, styles, and markup that are shared across multiple pages. By using a master page, you can maintain a consistent look and feel throughout your application, improve code reusability, and simplify maintenance.
Here's an example to illustrate how to use a master page in ASP.NET:
-
Create a master page:
Create a new ASP.NET web form (.aspx file) and specify it as a master page. Let's name it "SiteMaster.master". In the master page, you can define the overall layout, structure, and common elements of your application. Here's a basic example:
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="SiteMaster.master.cs" Inherits="YourNamespace.SiteMaster" %>
<!DOCTYPE html>
<html>
<head>
<title>My ASP.NET Application</title>
</head>
<body>
<div id="header">
<h1>My ASP.NET Application</h1>
</div>
<div id="content">
<asp:ContentPlaceHolder ID="MainContent" runat="server"></asp:ContentPlaceHolder>
</div>
<div id="footer">
© 2023 My ASP.NET Application. All rights reserved.
</div>
</body>
</html>
In this example, the master page contains a header, a content area (represented by the <asp:ContentPlaceHolder> control), and a footer.
-
Create a content page:
Create a new ASP.NET web form (.aspx file) that will inherit from the master page. Let's name it "Default.aspx". In the content page, you can define the unique content specific to that page. Here's a basic example:
<%@ Page Title="" Language="C#" MasterPageFile="~/SiteMaster.master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="YourNamespace.Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<h2>Welcome to My ASP.NET Application</h2>
<p>This is the home page of my application.</p>
</asp:Content>
In this example, the content page references the master page through the 'MasterPageFile' attribute. The unique content is defined within the '<asp:Content>'control, which fills the content area specified in the master page.
-
Customize the master page:
You can customize the master page by adding additional controls, styles, or markup. The content pages that inherit from the master page will automatically include these customizations. For example, you can add a navigation menu or a sidebar to the master page, and all content pages will have that consistent navigation throughout.
-
Run and test your application:
Build and run your application to see how the master page and content pages work together. The content page will be displayed within the layout defined in the master page, resulting in a consistent look and feel across multiple pages.
By using a master page, any changes made to the common layout or structure in the master page will automatically apply to all the content pages that inherit from it, reducing duplication and making maintenance easier.