Master Page:: Source: Mindcracker Network

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Source : Mindcracker Network (www.c-sharpcorner.

com) 

How to create Master page in ASP.NET 2.0


 

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.
Thank you for using Mindcracker Network

You might also like