ASP.NET - SPLessons

ASP.NET Master Pages

Home > Lesson > Chapter 20
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

ASP.NET Master Pages

ASP.NET Master Pages

shape Description

Master page is a page having layouts, navigation and functionality, which is used to provide same look and feel for all the pages of a Web Application. In ASP.NET, master page has a property called content place holder. The pages, which all need to inherit the properties of master page, are placed in between the ContentPlaceHolder tags. Master page has the extension “.master”.

How to Create a Master Page

shape Step 1

Let's create a Master Page. first, to create a Master Page one have to follow below steps. Go to 'Solution Explorer' and right click on the solution to add the master page. Now, select 'Add' -> go to 'New Item' and click on it.

shape Step 2

Now, a pop-up will appear asking to choose the item want to add. Select 'Master Page'. Give a 'Name' to it and click on 'Add'.

shape Step 3

Now, the master page is added to the solution and changes can be made according to the requirements. Put a title something like "Welcome to SPLessons". It should appear like below.

Create a New Web Form

shape Steps

Create a new web form using this Master Page. To create let's follow the below steps.

shape Step 1

Go to 'Solution Explorer' and right click on the solution to add the master page. Now select 'Add'-> go to 'New Item' and click on it.

shape Step 2

Now, a pop-up will appear asking, to choose the item want to add. Select 'Web Form using Master Page'. Give a 'Name' to it and click on 'Add'.

shape Step 3

One pop-up will appear asking to choose one Master Page, which one want's to use. Select as per choice and click on 'Ok' and for now let's choose 'SPMaster.Master'.

shape Step 4

Now, One have the new web form using the master page 'SPMaster.Master'. This new webform will inherit all the properties of that Master Page.

shape Syntax

Let’s take one master page name “SPMaster.master”. The syntax for master page is below. [html] <%@ Master %> <html> <body> <h2> Welcome to SPLessons </h2> <asp:ContentPlaceHolder id="spContentPlaceHolder " runat="server"> </asp:ContentPlaceHolder> </body> </html> [/html]

shape Example

In the below example, one can see how a page is placed under one master page. [html] <%@ Page MasterPageFile=" SPMaster.master " %> <asp:Content ContentPlaceHolderId="CPH1" runat="server"> <h3>New Page</h3> <table> <tr> <td>New Page No.</td> <td>1</td> </tr> </table> </asp:Content> [/html] The above code results in a page, where at the top “Welcome to SPLessons” text appears due to inheritance of master page and under that new page’s own contents are displayed, “New Page” text appears and in a table in one cell, a text appears “New Page No.” and in other cell, a text appears “1”.