Simple Master Page Example

I meant to revist my statement about finally getting Master Pages in Whidbey.  I am not going to go into all the details about Master Pages.  There are plenty of web sites and articles that describe that.  Here is a simple example.

The master page I created looks like the following:

<%@ Master Language="C#" CompileWith="MasterPage.master.cs" ClassName="MasterPage_master" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
 <head runat="server">
  <title>Master Page Sample</title>
 </head>
 <body>
  <form id="form1" runat="server">
   <div>
    <asp:contentplaceholder id="cpHolder" runat="server">
           </asp:contentplaceholder> <BR />
   </div>
   <p align="center">Copyright 2004<br />
    Wallace B. McClure<br />
    Knoxville, TN 37922<BR />
   </p>
  </form>
 </body>
</html>

In this simple example, there is a new control that you see.  This is the Content Place Holder (cph) control.  The cph control is merely a place holder  that is used to define a place in the master control where content is going to go.  

Now that we have the master page, how do you apply this to an aspx page?  Here is an example page.

<%@ Page Language="C#" MasterPageFile="~/Include/Server/MasterPage.master" CompileWith="Results.aspx.cs" ClassName="Results_aspx" Title="Test Page" %>
<
asp:Content runat=“server“ ContentPlaceHolderID="cpHolder" ID="ContentId">this is a test help me.........
<asp:Button ID="Button1" Runat="server" Text="Button" />.............</asp:Content>

That is all that the aspx page needs.  Note all the “stuff” that is not on this page.  There is not html page definition or anything that looks like a “regular” webform. There is the MasterPageFile item listed at the top of the page which tells the system to go grab the master page and put it together with this file when it is processed by ASP.NET.  There is a control on this page called the Content control.  This control is where you are going to put code that makes this page this page.  You can put controls within the Content control and the resulting page acts just like a regular aspx webform.  In this quicky example, the page acts and behaves just like one would expect.  To make the situation even better, the VS.NET IDE has full support for master pages. 

So, there is my simple master page example.  I hope that you find this helpful.

Wally

1 Comment

  • I have created a master page and the content page contains a button. The onclick method simply attempts to write hello to the page. The problem is that when you click on the button nothing happens at all. Have you any idea why?

Comments have been disabled for this content.