Introduction Of Master Page
Microsoft ASP.NET 2.0 introduces a new feature .Master Pages. that permits you to define common user interface (UI) elements and common structure in a template that can be applied to multiple content pages in a Web application. Standard elements are, for example, navigational menus, logos, and other stock graphical elements. This allows you to have a common appearance and functionality across your Web site, avoids duplication of code, and makes the site easier to maintain. If the content pages are based on the master page, all the elements defined in the master page would automatically be defined in the content pages.
The page layout for Master Pages is easy to create, simple to maintain, and simple to assign to a Web application. The Master page provides a single point of reference for all pages to display standardized Web content. Master pages are completely transparent to end users and permit developers to create Web sites where pages share a common layout. Defining a Master page is similar to defining a normal page, but saving a Master page is different from saving a normal page. You must save the Master pages by using the .master file extension.
How to Add Master Page in Your Application
Adding a master page to your web application is straight forward. Just right click on the project and select "Add New Item" and than select "Master Page". This will add the master page to your project.
The master page already has the content place holder control which is used to hold and display your contents. Let's delete that content placeholder and add it by our self. In this case we will create two content place holders. One will be on the left and other one on the right .After you insert the content placeholder control inside your table your master page will look something like this:
Using the master page in your aspx pages
Just add a new aspx page and name it as "first .aspx". Now you want to use the Sample1.master file in your aspx page. Just go to the html view of your page and add a Master Page File attribute in the page directive and delete all the other html that is written in the aspx page. The Master Page File attribute denotes that the Page is inheriting from the master page.
<%@ Page Master Page virtual="~/Sample1.master" %>
Difference between Master page and Other page
- The general syntex to write the directive in master page is:
<% @ Master Language = %>
The general syntex to write the directive in other page is:
<% @ Page Language = %>
- In Master page we use control
<asp: ContentPlaceHolder> </asp: ContentPlaceHolder>
<asp: Content> </asp: Content>
- The extension of master page is ".master".
The extension of other page is ".aspx".
Example of Master Page
Coding for Master Page
<% @ Master Language = "C#"> <Html> <Body> <Form id = "form1" runat = "Server" Style = "Font-family:Arial, Helvetica, sans-serif; font-size: 26px;
font-weight: bold; color: # ooffoo"> <div> <asp: ContentPlaceHolder id = "C1" runat = "server"> <asp: Label id= "label1" runat = "Server" Text = "Header"></asp:label> </asp: ContentPlaceHolder> </div> <br/> <br/> <div> <asp: ContentPlaceHolder id = "R4R" runat = "Server"> </asp: ContentPlaceHolder> </div> <br/> <br/> <div> <asp: ContentPlaceHolder id = "C2" runat = "Server"> <asp: label id = "label2" runat = "Server" Text = "Footer"> </asp: label> </asp: ContentPlaceHolder> </Form> </Body> </Html>
Coding for Web Page:
<% @ Page Language = "C#" MasterPageFile = "~/MasterPage.master"%> <asp: Content ContentPlaceHolder id = R4R runat = "Server"> </asp: Content>