Click here to Skip to main content
13,097,754 members (54,895 online)
Click here to Skip to main content
Add your own
alternative version


26 bookmarked
Posted 8 Apr 2006

MasterPages for .NET 1.1 (again)

, 8 Apr 2006
Rate this:
Please Sign up or sign in to vote.
Prepare your .NET 1.x site for the use of MasterPages in .NET 2.0

Sample Pages A and B


This article is about imitating the functionality of the Master Pages in the .NET 2.0 framework as close as possible. By doing this, your .NET 1.x site would be able adapt the Master Pages concept once you have converted your site.


I am working on a project which intended to use the .NET 2.0 framework, however early on we received word that the company was not quite ready for the switch. We really wanted to use things like master pages and so on. A quick search for master pages in .NET 1.1 resulted in a hand full of articles but none seemed really easy to use or mimic the functionality of master pages in .NET 2.0. 

Using the code

So, I took the approach of taking a masterpage.master from a .NET 2.0 app and used that as the entry point for my solution.

The ContentPlaceHolder controls in .NET 2.0 don’t exist in .NET 1.x so I replaced them with the Panel controls (asp:Panel).

The Master Pages contain the complete HTML layout, in other words everything inthe  Master Page is meant to “force” the layout on the calling page while preserving the content of the asp:Panel's (a.k.a ContentPlaceHolder controls) found on the page.

As you can see below, the html layout is what is left over with some default text as the content of the panels. I highlighted the panels so you can compare them with the panels in the calling page which I will discuss a little later. 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <title runat="server">Default1</title>
    <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <link href="Styles/MasterPage_A.css" type="text/css" rel="stylesheet" />
<body ms_positioning="GridLayout">
    <form id="Form1" method="post" runat="server">
        <table width="700" cellspacing="0" cellpadding="0" border="0" height="100%" align="center"

                <td class="Header" colspan="3">
                    <asp:Panel ID="pnlHeader" runat="server">
                        This is MasterPage A, set by the master page.<br />
                        Place a panel with ID="pnlHeader" in the Default.aspx and this text will be replaced.</asp:Panel>
                <td class="LeftMenu" valign="top">
                    <asp:Panel ID="pnlLeftMenu" runat="server">
                        This text will not show up as it is overridden by the page.
                <td class="Content">
                    <asp:Panel ID="pnlContent" runat="server">
                        This text will not show up as it is overridden by the page.
                <td class="RightMenu" valign="bottom">
                    <asp:Panel ID="pnlRightMenu" runat="server">
                        This text will not show up as it is overridden by the page.
                <td class="Footer" colspan="3">
                    <asp:Panel ID="pnlFooter" runat="server">
                        Footer area, set by the master page

As you can see this resembles a possible site layout (simple for this article), with some default text in the Panel's. These Panels will be replaced with the matching Panels found in the calling pages.

I had done other sites where I took HTML pages and used the ParseControl method to turn the html into a Control. Once you have a control, you can go through the controls collection of this control to, well…. find more controls.

I thought that should be possible to use this technique to replace one panel with another.

As I stripped out the content in the Master Page I did the reversed in the calling page (in this case the default.aspx). This means that only the html markup which actually belongs to the page (buttons, labels, panels, user controls and so on) stays while the site layout is safely stored in the master page. Again, mimicking the functionality of the Master Pages in .NET 2.0.

<P><%@ Page Language="c#" Codebehind="Default.aspx.cs" AutoEventWireup="false" Inherits="Default" %>

</P><P><asp:panel id="pnlLeftMenu" runat="server">
    This goes in the left menu
<asp:panel id="pnlContent" runat="server">
            <TD noWrap width="60">
                <asp:Button id="Button1" runat="server" Text="Show Time"></asp:Button></TD>
            <TD width="100%">
                <asp:Label id="Label1" runat="server" Text=""></asp:Label></TD>
<asp:panel id="pnlRightMenu" runat="server">
 This goes in the Right menu
<asp:panel id="Panel1" runat="server">
 This panel is ignored and will not be shown on the final page

As you can see in the HTML markup above, the site layout is missing, leaving only the Panel's (ContentPlaceHolders) and their content in the page.

Please note the naming of the Panel's in both the Page and the Master Page above. The ID's of the panels can be anything as long as they match between the two pages.

How it works

The solution to merge the Master Page with the calling Page consists of only five (simple) steps:
1) Collect all Panel controls within Page control collection.
2) Delete all the controls on the Page (aspx),
3) Load the Master Page from cache if exist or from disk if not in cache (prevents file locking on high trafic sites),
4) Replace the Panel controls found in the MasterPage with the Panel controls collected in step 2,
5) Add the Updated MasterPage (control) to the Page control (which was cleared in step 2).

Panels defined in the Master Page but not found in the calling page (aspx) will be shown as is, just like in .NET 2.0.

The only thing you need to do in the code behind file of the calling page is add the using System.Web.UI.preMasterPages directive and derive from the MasterPages class.

using System;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI.preMasterPages;

/// <span class="code-SummaryComment"><SUMMARY>
</span>/// Summary description for Default.
/// <span class="code-SummaryComment"></SUMMARY>
</span>public class Default : MasterPages
    private void Page_Load(object sender, System.EventArgs e)
        Title = "My Page Demo";
    ... The rest of the class is omitted for clarity

I added simply a MasterPage element to the web.config with the path and filename of the Master Page to use. You can easily change the code to allow for a "on-the-fly-pick-and-choose" approach.

I simply added a Title property in the base class wich sets the Title of the page on Page_Load.

You should be able to run your converted page without any code changes, I tried it with dynamically loaded user controls and while persisting state and it all works fine.

Once you are ready to migrate your site, you simply change the asp:Panel controls to asp:ContentPlaceHolder in both the aspx and the master page and you are done.

Have fun,


  • 09-04-2006
    • Changed the Code so it has only the Panels in the calling page (aspx) and the complete site layout in the MasterPage.master pages.
      Also added a Title property to the base class so the Title of the page can be set on Page_Load.
  • 07-04-2006
    • Initial Release


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


About the Author

W vanEck
Web Developer
Canada Canada
Wouter is currently working for Fujitsu Consulting [Canada] Inc in Calgary, Alberta, Beautiful Canada.
He has been working with the .NET framework since the early days, so consider him a dinosaur.
If he is not programming you can find him out in the field flying his F3A Pattern plane.

You may also be interested in...

Comments and Discussions

GeneralButton question Pin
ICTech26-Jun-07 17:36
memberICTech26-Jun-07 17:36 
GeneralRe: Button question Pin
W vanEck26-Jun-07 18:50
memberW vanEck26-Jun-07 18:50 
GeneralRe: Button question Pin
ICTech27-Jun-07 3:58
memberICTech27-Jun-07 3:58 
GeneralRe: Button question Pin
W vanEck27-Jun-07 4:02
memberW vanEck27-Jun-07 4:02 
GeneralRe: Button question Pin
ICTech27-Jun-07 5:46
memberICTech27-Jun-07 5:46 
GeneralRe: Button question Pin
W vanEck27-Jun-07 5:54
memberW vanEck27-Jun-07 5:54 
Generalerror in content page Pin
veerbala18-Apr-07 2:47
memberveerbala18-Apr-07 2:47 
GeneralRe: error in content page Pin
W vanEck26-Jun-07 18:49
memberW vanEck26-Jun-07 18:49 
GeneralSource Code for VB Version [modified] Pin
mazadel30-Jun-06 2:39
membermazadel30-Jun-06 2:39 
QuestionDesigner support? Pin
versat147412-Apr-06 5:49
memberversat147412-Apr-06 5:49 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170813.1 | Last Updated 8 Apr 2006
Article Copyright 2006 by W vanEck
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid