Click here to Skip to main content
13,001,686 members (86,532 online)
Click here to Skip to main content
Add your own
alternative version


43 bookmarked
Posted 13 Feb 2005

Master Page solution just like in ASP.NET 2!

, 13 Feb 2005
Rate this:
Please Sign up or sign in to vote.
How to add support to master page programming?

Sample Image - MasterPage.jpg


In this article, we can see a simple solution for the master page coding . In future, update to framework 2 will be simple, since the solution is very similar to the new framework design.

Using the code

All we need to do for the master-page to work is:

  • Derive our page from the master page.
  • Enclose all the page in a tag named "content".
  • Leave the form name "Form1", as it come by default.
  • Use a user control as the master, and include in its HTML code asp:placeholder named "ContentPlaceHolder".

How does it work?

The master user control, has a tree of controls, one of them is the placeholder named "ContentPlaceHolder". On the other hand, the page itself has a tree of controls, one of them is the "content" (actually it has a tree under the list: Form1->content).

The process is:

  • Take the content, and move it under the placeholder in the master user control.
  • Take the hole master user control and move it to under the Form.

That is all.

The code

The Master Page code:

public class MasterPage : System.Web.UI.Page
        public MasterPage() : base()
            this.Init +=new EventHandler(MasterPage_PreInit);
        private void MasterPage_PreInit(object sender, EventArgs e)
            //LOAD TEMPLATE
            //Replace path with aplication parameter!

            UserControl template = 

            //LOAD CONTENT

            Control content = this.FindControl("content");





Here is a Master User Control. The only thing we must do is to include the placeholder named ContentPlaceHolder.

<%@Control Codebehind="MasterTemplate.ascx.cs" 

           Inherits= "MasterPage.MasterTemplate" ... %>
<TABLE id="Table1"  runat="server">
      <TD height="22" align="center">This Is The Header</TD>

   <TD vAlign="top" align="right"> 
     <tableid="table2" runat="server">
           <asp:placeholder id="ContentPlaceHolder" 

      <tr height= "10>

               Runat= "server">Fatal error...</asp:label>

  <tr height="10"> 
     <td align="center">This Is The Bottom</td>  

And here is the page. All the code rendered in the BODY must be enclosed in a tag named content.

<%@ Page ...  %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
        <form id="Form1" method="post" runat="server">
            <asp:panel id="content" runat="server" HorizontalAlign="Center">
                <asp:Calendar id="Calendar1" runat="server" ... ></asp:Calendar>

And here is the page code behind. The only thing we must do is to derive it from the base page, MasterPage.

public class WebForm1 : MasterPage

Move to ASP.NET 2

To change our code to run under the new framework we will need to:

  • remove the call to the method MasterPage_PreInit.
  • call
    Page.MasterPagePage.MasterPageFile = "~/MasterPage.master";

Or add in the page directive:

<%@ Page MasterPageFile="~/MasterPage.master" ... %>

The new view of Master Page in ASP.NET 2:

Other articles obout master page:


The code in this article is based on a presentation of Alik Levin from Microsoft.

Visit Yitzhak Gootvilig's blog.


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

Yitzhak Gootvilig
Architect Matrix-IT
Israel Israel
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralValidators not working Pin
toxaq27-Mar-06 12:52
membertoxaq27-Mar-06 12:52 
GeneralRe: Validators not working Pin
toxaq27-Mar-06 15:38
membertoxaq27-Mar-06 15:38 
GeneralRe: Validators not working Pin
Muhammad Shehabeddeen7-May-06 7:12
memberMuhammad Shehabeddeen7-May-06 7:12 
SDP2-Sep-05 5:19
memberSDP2-Sep-05 5:19 
GeneralExpandability Pin
guiM7629-Jul-05 1:21
memberguiM7629-Jul-05 1:21 
GeneralHard Coded Name Pin
Mehfuz Hossain26-Jun-05 5:57
memberMehfuz Hossain26-Jun-05 5:57 
GeneralRe: Hard Coded Name Pin
kalyankrishna124-May-06 7:02
memberkalyankrishna124-May-06 7:02 
GeneralRe: Hard Coded Name Pin
Mehfuz Hossain24-May-06 7:44
memberMehfuz Hossain24-May-06 7:44 
GeneralRe: Hard Coded Name Pin
kalyankrishna124-May-06 20:42
memberkalyankrishna124-May-06 20:42 
GeneralFindControl doesn't work! Pin
[--Pete--]13-Jun-05 4:29
member[--Pete--]13-Jun-05 4:29 
GeneralRe: FindControl doesn't work! Pin
[--Pete--]13-Jun-05 23:32
member[--Pete--]13-Jun-05 23:32 
Questionerror in global.asax? Pin
Bouma12-Apr-05 10:22
memberBouma12-Apr-05 10:22 
AnswerRe: error in global.asax? Pin
SDP2-Sep-05 5:04
memberSDP2-Sep-05 5:04 
GeneralAccessing LabelError from the Master Page Pin
musaev29-Mar-05 5:24
membermusaev29-Mar-05 5:24 
GeneralThese articles make me nutz! Pin
Anonymous18-Feb-05 10:19
sussAnonymous18-Feb-05 10:19 
GeneralRe: These articles make me nutz! Pin
Misty_Blue22-Feb-05 9:37
memberMisty_Blue22-Feb-05 9:37 
GeneralRe: These articles make me nutz! Pin
Johan Trenkle19-Apr-05 21:12
sussJohan Trenkle19-Apr-05 21:12 
GeneralUnfortunate side-effort Pin
Mark Macaulay16-Feb-05 15:03
sussMark Macaulay16-Feb-05 15:03 
GeneralRe: Unfortunate side-effort Pin
Yitzhak Gootvilig20-Feb-05 20:05
memberYitzhak Gootvilig20-Feb-05 20:05 Pin
Simon Thompson15-Feb-05 6:13
memberSimon Thompson15-Feb-05 6:13 
GeneralRe: Pin
Simon Thompson15-Feb-05 6:47
memberSimon Thompson15-Feb-05 6:47 
GeneralGot it to work...Great...but problems with IDE Pin
pchelp15-Feb-05 4:47
memberpchelp15-Feb-05 4:47 
GeneralThanks Yitzhak Pin
Simon Hughes15-Feb-05 0:10
memberSimon Hughes15-Feb-05 0:10 
Generalabout time Pin
Simon Thompson14-Feb-05 22:51
memberSimon Thompson14-Feb-05 22:51 
GeneralThis is working! Pin
Alexander Turlov14-Feb-05 9:20
memberAlexander Turlov14-Feb-05 9:20 

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
Web01 | 2.8.170624.1 | Last Updated 13 Feb 2005
Article Copyright 2005 by Yitzhak Gootvilig
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid