Blue Theme Orange Theme Green Theme Red Theme
 
Nevron Gauge for SharePoint
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Team Foundation Server Hosting
Search :       Advanced Search »
Home » ASP.NET 2.0/3.5 » How to create Master page in ASP.NET 2.0

How to create Master page in ASP.NET 2.0

This article enables you to understand the concept of Master page.

Author Rank :
Page Views : 74109
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

Master page:

 

Master page enable you to share the same content among multiple content pages in a website. We can use master page to create a common page layout.

 

For Example: If you want all the pages in your website to share a three column layout, you can create the layout once in a master page and apply the layout to multiple content pages.

 

You can make your website easier to maintain, extend and modify by using master page. If you want to display a standard header and footer in each page in your website, then we can create the standard header and footer in a master page.

 

If you want to add a new page in your website that looks just like the other pages in your website, then we simply need to apply the same master page to the new content page.

 

You can completely modify the design of your website without change every content page. You can modify just a single master page to dramatically change the appearance of all the pages in your application.

A Master Page can contain the same Web controls, User controls, HTML content, and scripts that can be added to a standard ASP.NET page.

Creating the Website:

If you have an existing web site, you can skip this section. Otherwise, you can create a new web site and page by following these steps.

To create a file system Web site:

  • Open Visual Studio 2005.
  • On the File menu, open New and click on Web Site.

The New Web Site dialog box appears.

  • Under Visual Studio installed templates, click ASP.NET Web Site.
  • In the Location box, enter the name of the folder where you want to keep the pages of your Web site.

For example, type the folder name C:\WebSites\mySite.

  • In the Language list, click Visual Basic (or the language you prefer to work with).
  • Click OK.

Visual Web Developer creates the folder and a new page named Default.aspx.

Creating master page:

You create a master page visual web developer by selecting the website menu option, add new item in server explore and selecting the master page option from the add new item page.

 

 

Figure: This figure represent how to select the master page

 

The source file of master page is as follows:

 

<%@ Master Language="VB" CodeFile="MasterPage.master.vb" Inherits="MasterPage" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">

        </asp:contentplaceholder>

    </div>

    </form>

</body>

</html>

 

You create a master page by creating a file that ends with the .master extension.

In the application you can locate a master page file any place. You can add multiple master pages to the same application.  

 

You can place almost all the same elements in a master page that you could place in asp.net page, including HTML, server side scripts and ASP.NET controls.

There are two special things about the master page, 

  • First notice that the first contains a <%@ Master %> directive instead of the normal <%@ Page %> directive.
  • Second notice that the master page includes two <contentplaceholder>. You can add as many <contentplaceholder> to a master page as you need.

<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">

</asp:contentplaceholder>

 

For Example: Now we are creating a master page. The source file of that master page's is as follows:

 

<%@ Master Language="VB" CodeFile="MasterPage.master.vb" Inherits="MasterPage" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head   runat="server">

    <title>My master page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div class="content">

       <div class="left column">        

         <asp:contentplaceholder id="contentplaceholder1" runat="server">

            <B/><I>IMPORTANT WEBSITE FOR LEARNING DOTNET      TECHNOLOGY</I><BR/><BR>

            asp.netheaven.com<br/>

            dotnetheaven.com<br/>

            c-sharpcorner.com<br/>

            vb.netheaven.com<br/>

         </asp:contentplaceholder>

       </div>

    <div class="right column">

        <asp:contentplaceholder ID="contentplaceholder2" runat="server">

           <B/><I>OUR NETWORK</I><BR/><BR/>

           longhorncorner.com<br/>

           interviewcorner.com<br/>

           mindcracker.com<br/>

           microgold.com<br/>

        </asp:contentplaceholder>

    </div><br class="clear"/></div>

    </form>

</body>

</html>

  

Output:

 

This page will open after debugging these codes.

 

 

 

Figure: My master page.

 

There are two contentplaceholder in the above page. We can add multiple contentplaceholder in the master page.

 

You can't cache a master page with the output cache directive. You also can't apply a theme to a master page.

 

Master page uses cascading style sheets (CSS) to create the page layout. HTML tables should be used only to display tabular information.

 

Run-time Behavior of Master Pages:

 

At run time, master pages are handled in the following sequence:

  • Users request a page by typing the URL of the content page.
  • When the page is fetched, the @ Page directive is read. If the directive references a master page, the master page is read as well. If this is the first time the pages have been requested, both pages are compiled.
  • The master page with the updated content is merged into the control tree of the content page.
  • The content of individual Content controls is merged into the corresponding ContentPlaceHolder control in the master page.
  • The resulting merged page is rendered to the browser.

Conclusions:

Master pages provide a key component to any web application, namely the ability to organize a consistent layout into reusable templates. These master page templates offer full designer support and programmatic interfaces to meet the demands of most applications.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Purushottam Rathore

I am working as a Software Developer and has 4 years of experience on Microsoft Technology and having a Master Degree in Computer Application. I really like to work in the .NET platform. and working with ASP.NET 2.0/3.5, Web Services, WPF, WCF, Silverlight, AJAX, JavaScript, JQuery, Ado.net, MsAccess, SQL Server 2005/2008.

Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Comments
how to design sitemap by sarita On September 10, 2008
thanks for joining me with u r community, i want some help of you. i want to know how to design a site map in master page.
Reply | Email | Modify 
Re: how to design sitemap by Purushottam On October 9, 2008

Hi

 First, you should require a file with the required contents to create a Sitemap. This file is in XML format. crete a web.sitemap like as follows.

<?xml version="1.0" encoding="utf-8" ?>
<
siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<
siteMapNode url="Default.aspx" title="Student record" description="Student record">
<
siteMapNode url="Default2.aspx" title="Friends" description="Friends">
<
siteMapNode url="MyFriends.aspx" title="My Friends" description="My Friends"/>
<
siteMapNode url="FriendRequest.aspx" title="Make friend" description="Make friend"/>
</
siteMapNode>
<
siteMapNode url="~/Courses/Default.aspx" title="Course" description="Course">
<
siteMapNode url="~/Courses/AllCourses.aspx" title="AllCourses" description="AllCourses"/>
<
siteMapNode url="~/Courses/InformationTechnology.aspx" title="InformationTechnology" description="InformationTechnology"/>
<
siteMapNode url="~/Courses/Management.aspx" title="Management" description="Management"/>
</
siteMapNode>
</
siteMapNode>
</
siteMap>

Now add site map into master page:

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<
head runat="server">
<title>Untitled Page</title>
</
head>
<
body>
<form id="form1" runat="server">
<div>
<table width="100%" cellpadding="2" cellspacing="0">
<tr>
<td>
<asp:Menu ID="Menu1" runat="server" DataSourceID="SiteMapDataSource1">
</asp:Menu>
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
</td>
</tr>
<tr height="50">
<td>
</td>
</tr>
<tr>
<td>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</td>
</tr>
</table>
</div>
</form>
</
body>
</
html>

Reply | Email | Modify 
good work by lakshmi On July 13, 2009
good explanation for beginners it will help a lot
Reply | Email | Modify 
Re: good work by Purushottam On July 13, 2009
Thanks lakshmi
Reply | Email | Modify 
Iage gallery by meet On April 4, 2010
I want to create image gallery. In this i want to add some categories of the images.I want to upload & download function in the image gallery.& if the user upload the image then it will comes to the perticular  categories.so how can i create this applicaton. Can u help me?
Reply | Email | Modify 
6 Months Free & No Setup Fees ASP.NET Hosting!
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.