Click here to Skip to main content
15,911,531 members
Articles / Web Development / ASP.NET
Article

master pages - The ASP.net 2.0 way

Rate me:
Please Sign up or sign in to vote.
1.26/5 (20 votes)
5 May 20063 min read 41.9K   20   3
Explores the nooks and cranies of master pages in asp.net 2.0

Introduction

One of the highlights of ASP.net 2.0 has been the ‘master pages’ feature.  Master Pages feature is one of the most power features that many developer have unsuccessfully tried to create alternatives for.  Almost none of the development platforms prior to ASP.net 2.0 have ever come close to implementing any thing even close to Master Pages.  However powerful and eligant few have indulge into understanding the full impact that Master Pages can bring about.  Therefore, in this article we will take a deep dive in the depths of Master Pages.

<o:p> 

What are Master Pages:

The idea of Master Pages was taken from PowerPoint.  PowerPoint has for years implemented the idea of MasterPages (called Slide Master in Power Point).  Basic idea is to reduce replication by putting all elements that are common in one page (master page) and build rest of the pages on top of master page.  In programming term think of master page as a virtual base class that can only be used for inheritance purposes.  Pacticle example:  instead of coping web-page header on every single page, just create the webpage header in master page and then create all the pages on top of (or inherit from) masterpage. 

<o:p> 

How does it works:

In master pages one can create content place holders.  Just as the name suggests, content place holders are place holders for content that will be provided by the pages that are build on top (or inherit) this master page.  Example:
In master page:  <asp:contentplaceholder id="Text" runat="server"/>
In web page:  <asp:content id="Content1" contentplaceholderid="Text" runat="server">Some text.</asp:content>

<o:p> 

Key Features of Master Pages:

  1. Reduces replication:  as described earlier, it reduces replication.
  2. Multiple inheritance or nesting:  There can be serveral master pages inheriting from (or nested within) each other. Example Master Page B inherits Master Page A.  Therefore, page gamma which directly inherits Master page B also inherits indirectly Master page A.  However, there is no design time support for this feature in visual Studio 2005, so expect to hand code this feature.  Code example for this: 
    Master Page A’s page header:  <%@ master language="C#" %>
    Master Page B’s page header:  <%@ master language="C#" masterpagefile="~/MP_A.master"%>
    Page Gamma’s page header:  <%@ page language="C#" masterpagefile="~/MP_B.master" %>
  3. Ability to programmatically access elements of master page from regular page’s code behind: 
    Suppose you are working on an Ecommerce app and the header of page has a cart sub-total user control.  The whole header can be put in the master page and the cart sub-total user control can be easily accessed through the page’s code behind.  Here is an example for that: 
    MasterPage m = Page.Master;
    Cart c = (Cart)m.FindControl("oCart");
    c.SubTotal = 45.33;
  4. Ability to specify default content:  If a web page does not provide content for one or more of the content place holder(s) then the default content is used.  The default content is placed in between the content place holder tags in the master page. 

<o:p> 

Conclusion:

As you see this is really a powerful feature.  One most obvious use for this feature is in the Ecommerce technology.  And from when I have seen around the web so far alim systems have used this technology to the fullest.  Hope you have learned from this episode of the ASP.net 2.0 Highlights.  Until next time, take care.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionerror in content page Pin
veerbala18-Apr-07 2:51
veerbala18-Apr-07 2:51 
I am creating a master page and then adding a content page to it. when i place controls(tags) like update panel or table or textbox or label in the content page(within the content tags) i get a red line under all the tags in that page. when i move my mouse over it, it says "Element UpdatePanel is not a known element This can occur if there is a compilation error in the website" . I have tried all possible alterations but in vain. please help me out. many thanks


balaji
AnswerRe: error in content page Pin
codemonkeyman218-Apr-07 9:53
codemonkeyman218-Apr-07 9:53 
GeneralMaster Page Code Pin
aschreiber11-Dec-06 14:18
aschreiber11-Dec-06 14:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.