i

ASP.Net A Complete Guide

ASP.NET Master Pages

Master Pages:

Master pages help us to create a consistent look for all the pages (or group of pages) in our web application. For a rich user interface, consistency is essential. Before the master page, we were required to create the pages and add the custom stylesheet and needed to write lots of code. With the master pages, we write the standard code and reuse that for creating a consistent look.

Below is some vital segment need to consider while creating the master pages:

ContentPlaceHolder: A reserved area for the content pages for rendering the contents.

ContentControl: The contents inside this control will be loaded where the MasterPage's ContentPlaceHolder is located.

Masterpage: Help us to create a standard UI.

ContentPage: The ASP.NET web pages we called as content pages.

Master pages are simply like our HTML pages designed as a standard template for other pages. The directive @Masterdefines it as a master page.

Master Page Example:

<%@Master %>

<html>

<body>

<h1>My common header</h1>

<asp:ContentPlaceHolder id="MasterPageId" runat="server">

</asp:ContentPlaceHolder>

</body>

</html>

 

Suppose the file name of the above content is "MyMaster.master"

Content Page:

This pages contains a content tag <asp:Content>. It will have a reference to the master page. These pages are the normal asp.NET pages, and it used master pages for common UI.

Content Page Example:

<%@ Page MasterPageFile="MyMaster.master" %>

<asp:Content ContentPlaceHolderId="MasterPageId" runat="server">

  <form runat="server">

    <asp:TextBox id="textId1" runat="server" />

    <asp:Button id="buttonId1" runat="server" text="Button" />

  </form>

</asp:Content