Master pages - ASP.NET


Master pages  ASP.NET

ASP.NET 2.0 Features
Developer Productivity
            Master Pages
            New Code-Behind Model in ASP.NET 2.0
            Creating & Sharing Reusable Components 
            New ASP.NET 2.0 Controls
                       Data Controls
                       Security Controls
                       Other New Controls
                       Validation Groups
            Web Parts Framework
            Visual Studio 2005 Improvements
Administration and Management
Speed and Performance
            Caching Feature
ASP.NET 2 Master Pages 

Professional ASP.NET 2.0 Databases

This excerpt from Professional ASP.NET 2.0 Databases   by Thiru Thangarathinam, is printed with permission from
Wrox Publication.
<<Previous  Next>>

Master Pages
A Master Page Example

Master Pages

ASP.NET 2.0 introduces a new concept known as master pages, in which a common base master file contains the common look and feel and standard behavior for all the pages in your application. Once the common content is placed in the master page, the content pages (child pages) can inherit content from the master pages apart from adding their content to the final output. To allow the content page to add its own content, you add placeholders (known as ContentPlaceHolder control) in the master page that will be utilized by the content pages to add their custom content. When users request the content pages, the output of the content pages are merged with the output of the master page, resulting in an output that combines the layout of the master page with the output of the content page.

Master pages are saved with the file extension .master. Apart from containing all the contents that are required for defining the standard look and feel of the application, the master pages also contain all the top-level HTML elements for a page, such as <html>, <head>, and <form>. As mentioned previously, the master pages also contain one or more content placeholders that are used to define regions that will be rendered through the content pages.

Now that you have had a general understanding of master pages, take a look at an example. First, create a master page named CommonPage.master and add the code shown in Listing 1-1.

Listing 1-1: A Master Page Example
<%@ master language=”C#” %>
     <head runat=”server”>
             <title>Master Page</title>
          <form runat=”server”>
                    Master Page Content
                 <asp:ContentPlaceHolder id=”MiddleContent” runat=”server”>

Apart from looking at the file extension, you can also identify a master file by looking at the new page directive named master at the top of the page. This declarative is used to identify that the current page is a master page and prevents users from requesting the page from a browser. Inside the code, the code contains an element named asp:ContentPlaceHolder, which will be used by all the content pages to render appropriate content that is specific to their pages. That’s all there is to creating the master page. To create a content page, add a new ASP.NET page named ContentPage.aspx and modify the code as follows:

<%@ page language=”c#” MasterPageFile=”~/CommonPage.master” %>
<asp:Content id=”Content1” ContentPlaceHolderID=”MiddleContent”
Child Page Content

The code required for the content page is very simple and straightforward. As part of the page directive, specify a new attribute named MasterPageFile that is used to identify the name of the master page that you want to utilize. This example uses the master page created in Listing 1-1. Next, you have a new element named asp:Content that is used to associate the asp:ContentPlaceHolder element in the master page with the content page. This is done through the use of the ContentPlaceHolderID attribute. That’s all that is there to creating a master page and using the master page from a content page. Now, if you request the content page from a browser, you will get the output that is produced by merging the master page with the content page.

<<Previous  Next>>

Write your comment - Share Knowledge and Experience

More Related links 

ASP.NET Server Control Events

A Master page offers a template for one or more web forms. It defines placeholders for the content, which can be overridden by the content pages. The content pages contains only content........

ASP.NET Server Control Events

Article on ASP.NET Server Control Events.

ASP.Net Server Control vs HTML control

This articles explains difference between Server Control and HTML control in ASP.NET.

ASP.NET Navigation Methods

This articles describes the navigation ways available in ASP.NET.

.Net Framework 

This includes introduction of .Net framework, .Net framework architecture, role of assembly and GAC.

What is impersonation in ASP.NET?

ASP.NET can also impersonate a specific account you specify in web.config.........

What is event bubbling in .NET?

The passing of the control from the child to the parent is called as bubbling..........

Describe how the ASP.NET authentication process works.

ASP.NET runs inside the process of IIS due to which there are two authentication layers which exist in the system.............


Latest MCQs
» General awareness - Banking » ASP.NET » PL/SQL » Mechanical Engineering
» IAS Prelims GS » Java » Programming Language » Electrical Engineering
» English » C++ » Software Engineering » Electronic Engineering
» Quantitative Aptitude » Oracle » English » Finance
Home | About us | Sitemap | Contact us | We are hiring