The extension of MasterPage is '.master'. MasterPage cannot be directly accessed from the client because it just acts as a template for the other Content Pages. In a MasterPage we can have content either inside ContentPlaceHolder or outside it. Only content inside the ContentPlaceHolder can be customized in the Content Page. We can have multiple masters in one web application.A MasterPage can have another MasterPage as Master to it. The MasterPageFile property of a webform can be set dynamically and it should be done either in or before the Page_PreInit event of the WebForm. Page.MasterPageFile = "MasterPage.master". The dynamically set Master Page must have the ContentPlaceHolder whose content has been customized in the WebForm.
A master page is defined using the following code,
<%@ master language="C#" %>
Adding a MasterPage to the Project
- Add a new MasterPage file (MainMaster.master) to the Web Application.
- Change the Id of ContentPlaceHolder in <Head> to "cphHead" and the Id "ContentPlaceHolder1" to "cphFirst".
- Add one more ContentPlaceHolder (cphSecond) to Master page.
- To the master page add some header, footer and some default content for both the content place holders.
- <form id="form1" runat="server"> Header...
- <br />
- <asp:ContentPlaceHolder id="cphFirst" runat="server"> This is First Content Place Holder (Default) </asp: ContentPlaceHolder>
- <br />
- <asp:ContentPlaceHolder ID="cphSecond" runat="server">
This is the second Content Place Holder (Default).
- </asp:ContentPlaceHolder>
- <br /> Footer...
- </form>