13 min read

Master Pages

Earlier you were introduced to a feature called Master Pages, but what exactly are they? The idea behind them is the one that’s been around since the early days of development. The idea that you can inherit the layout of one page for use in another is the one that has kept many developers scrambling with Includes and User Controls. This is where Master Pages come into play. They allow you to lay out a page once and use it over and over. By doing this, you can save yourself countless hours of time, as well as being able to maintain the look and feel of your site from a single place. By implementing a Master Page and using ContentPlaceHolders, your page is able to keep its continuity throughout.

You’ll see on the Master Page (SimpleCMS.master) that it looks similar to a standard .aspx page from ASP.NET, but with some slight differences. The <@…> declaration has had the page identifier changed for a Master declaration. Here is a standard web page declaration:

<%@ Page Language="VB" MasterPageFile="~/SimpleCMS.master"
AutoEventWireup="false" CodeFile="Default.aspx.vb"
Inherits="_Default" Title="Untitled Page" %>

Here is the declaration for a Master Page:

<%@ Master Language="VB" CodeFile="SimpleCMS.master.vb"
Inherits="SimpleCMS" %>

This tells the underlying ASP.NET framework how to handle this special page. If you look at the code for the page, you will also see that it inherits from System.Web.UI.MasterPage instead of the standard System.Web.UI.Page. They function similarly but, as we will cover in more detail later, they have a few distinct differences.

Now, back to the Master Page. Let’s take a closer look at the two existing ContentPlaceHolders. The first one you see on the page is the one with the ID of “Head”. This is a default item that is added automatically to a new Master Page and its location is also standard. The system is setting up your page so that any “child” page later on will be able to put things such as Javascript and style tags into this location. It’s within the HTML <head> tag, and is handled by the client’s browser specially. The control’s tag contains a minimal amount of properties-in reality only four, along with a basic set of events you can tie to. The reason for this is actually pretty straightforward – it doesn’t need anything more. The ContentPlaceHolder controls aren’t really meant to do much, from a programming standpoint. They are meant to be placeholders where other code is injected, from the child pages, and this injected code is where all the “real work” is meant to take place. With that in mind, the system acts more as a pass-through to allow the ContentPlaceHolders to have as little impact on the rest of the site as possible.

Now, back to the existing page, you will see the second preloaded ContentPlaceHolder (ContentPlaceHolder1). Again, this one will be automatically added to the new Master Page when it’s initially added. Its position is really more of just being “thrown on the page” when you start out. The idea is that you will position this one, as well as any others you add to the page, in such a way as to complement the design of your site. You will typically have one for every zone or region of your layout, to allow you to update the contents within. For simplicity sake, we’ll keep with the one zone approach to the site, and will only use the two existing preloaded ContentPlaceHolders for now at least.

The positioning of ContentPlaceHolder1 in the current layout is one where it encapsulates the main “body” for the site. All the child pages will render their content up into this section. With that, you will notice the fact that the areas outside this control are really important to the way the site will not only look but also act. Setting up your site headers (images, menus, and so on) will be of the utmost importance. Also, things such as footers, borders, and all the other pieces you will interact with on each page are typically laid out on your Master Page. In the existing example, you will also see the LoginStatus1 control placed directly on the Master Page. This is a great way to share that control and any code/events you may have tied to it, on every page, without having to duplicate your code.

There are a few things to keep in mind when putting things together on your Master Page. The biggest of which is that your child/content page will inherit aspects of your Master Page. Styles, attributes, and layout are just a few of the pieces you need to keep in mind. Think of the end resulting page as more of a merger of the Master Page and child/content page. With that in mind, you can begin to understand that when you add something such as a width to the Master Page, which would be consumed by the children, the Child Page will be bound by that.

For example, when many people set up their Master Page, they will often use a <table> as their defining container. This is a great way to do this and, in fact, is exactly what’s done in the example we are working with. Look at the HTML for the Master Page. You will see that the whole page, in essence, is wrapped in a <table> tag and the ContentPlaceHolder is within a <td>. If you were to happen to apply a style attribute to that table and set its width, the children that fill the ContentPlaceHolder are going to be restricted to working within the confines of that predetermined size. This is not necessarily a bad thing. It will make it easier to work with the child pages in that you don’t have to worry about defining their sizes-it’s already done for you, and at the same time, it lets you handle all the children from this one location. It can also restrict you for those exact same reasons. You may want a more dynamic approach, and hard setting these attributes on the Master Page may not be what you are after. These are factors you need to think about before you get too far into the designing of your site.

Now that you’ve got a basic understanding of what Master Pages are and how they can function on a simple scale, let’s take a look at the way they are used from the child/content page. Look at the Default.aspx (HTML view). You will notice that this page looks distinctly different from a standard (with no Master Page) page. Here you have what a page looks like when you first add it, with no Master Page:

<%@ Page Language="VB" AutoEventWireup="false"
CodeFile="Default2.aspx.vb" Inherits="Default2" %>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>

Compare this to a new Web Form when you select a Master Page.

<%@ Page Language="VB" MasterPageFile="~/SimpleCMS.master"
AutoEventWireup="false" CodeFile="Default2.aspx.vb"
Inherits="Default2" title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head"
Runat="Server">
</asp:Content>
<asp:Content ID="Content2"
ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
</asp:Content>

You will see right away that all the common HTML tags are missing from the page with a Master Page selected. That’s because all of these common pieces are being handled in the Master Page and will be rendered from the Master Page. You will also notice that the page with a Master Page also has an additional default attribute added to its page declaration. The title attribute is added so that, when merged and rendered with the Master Page, the page will get the proper title displayed. In addition to the declaration tag differences and the lack of the common HTML tags being absent, the two ContentPlaceHolder tags we defined on the Master Page are automatically referenced through the use of a Content control. These Content controls tie directly to the ContentPlaceHolder tags on the Master Page through the ContentPlaceHolderID attribute. This tells the system where to put the pieces when rendering. The basic idea is that anything between the opening and closing tags of the Content control will be rendered out to the page when being called from a browser.

Themes

Themes are an extension of another idea, like Master Pages, that has kept developers working long hours. How do you quickly change the look and feel of your site for different users or usages? This is where Themes come in. Themes can be thought of as a container where you store your style sheets, images, and anything else that you may want to interchange in the visual pieces of your site. Themes are folders where you put all of these pieces to group them together. While one user may be visiting your site and seeing it one way, another user can be viewing the exact same site, but get a completely different experience.

Let’s start off by enabling our site to include the use of Themes. To do this, right-click on the project in the Solutions Explorer, select Add ASP.NET Folder, and then choose Theme from the submenu:

ASP.NET 3.5 CMS: Master Pages, Themes, and Menus

The folder will default to Theme1 as its name. I’d suggest that you name this something friendlier though. For now, we will call the Theme as “SimpleCMSTheme”. However, later on you may want to add another Theme and give your folders descriptive names, which will really help you keep your work organized.

You will see that a Theme is really nothing more than a folder for organizing all the pieces. Let’s take a look at what options are available to us. Right-click on the SimpleCMSTheme folder we just created, select Add New Item, and you should see a list similar to this one:

ASP.NET 3.5 CMS: Master Pages, Themes, and Menus

Your items may vary depending on your installation, but the key items here are Skin File and Style Sheet. You may already be familiar with stylesheets if you’ve done any web design work, but let’s do a little refresher just in case. Stylesheets, among other uses, are a way to organize all the attributes for your HTML tags. This is really the key feature of stylesheets. You will often see them referenced and called CSS, which stands for Cascading Style Sheets that I’ll explain in more detail shortly, but it’s also the file extension used when adding a stylesheet to your application. Let’s go ahead and add Style Sheet to our site just like the example above. For our example, we’ll use the default name StyleSheet.css that the system selects. The system will preload your new stylesheet with one element-the body{} element. Let’s go ahead and add a simple attribute to this element. Put your cursor between the open “{” and close “}” brackets and press Ctrl+space and you should get the IntelliSense menu. This is a list of the attributes that the system acknowledges for addition to your element tag. For our testing, let’s select the background-color attribute and give it a value of Blue. It should look like this when you are completed:

body {
background-color: Blue;
}

Go ahead, save your stylesheet, run the site, and see what happens. If you didn’t notice any difference, that’s because even though we’ve now created a Theme for the site and added an attribute to the body element, we’ve never actually told the site to use this new Theme. Open your web.config and find the <pages…> element. It should be located in the <configuration><system.web>  section, as shown next:

ASP.NET 3.5 CMS: Master Pages, Themes, and Menus

Go ahead, select the <pages> element, and put your cursor right after the “s”. Press the spacebar and the IntelliSense menu should show up like this:

ASP.NET 3.5 CMS: Master Pages, Themes, and Menus

You will see a long list of available items, but the item we are interested in for now is the theme. Select this and you will be prompted to enter a value. Put in the name of the Theme we created earlier.

<pages theme="SimpleCMSTheme">

We’ve now assigned this Theme to our site with one simple line of text. Save your changes and let’s run the site again and see what happens. The body element we added to our stylesheet is now read by the system and applied appropriately. View the source on your page and look at how this code was applied. The following line is now part of your rendered code:

<link href="App_Themes/SimpleCMSTheme/StyleSheet.css"
type="text/css" rel="stylesheet" />

Now that we’ve seen how to apply a Theme and how to use a stylesheet within it, let’s look at one of the other key features of the Theme, the Skin file. A Skin file can be thought of as pre-setting a set of parameters for your controls in your site. This will let you configure multiple attributes, in order to give a certain look and feel to a control so that you can quickly reuse it at any time. Let’s jump right in and take a look at how it works, to give you a better understanding. Right-click on the SimpleCMSTheme folder we created and select the Skin File option. Go ahead and use the defaulted name of SkinFile.skin for this example. You should get an example like this:

<%--
Default skin template. The following skins are provided as examples only.
1. Named control skin. The SkinId should be uniquely defined because
duplicate SkinId's per control type are not allowed in the same theme.
<asp:GridView runat="server" SkinId="gridviewSkin" BackColor="White" >
<AlternatingRowStyle BackColor="Blue" />
</asp:GridView>
2. Default skin. The SkinId is not defined. Only one default
control skin per control type is allowed in the same theme.
<asp:Image runat="server" ImageUrl="~/images/image1.jpg" />
--%>

We now have the default Skin file for our site. Microsoft even provided a great sample here for us. What you see in the example could be translated to say that any GridView added to the site, with either no SkinID specified or with a SkinID of gridviewSkin, will use this skin. In doing so, these GridViews will all use a BackColor of White and AlternatingRowsStyle BackColor of Blue. By putting this in a Skin file as part of our Theme, we could apply these attributes, along with many others, to all like controls at one time. This can really save you a lot of development time. As we go through designing the rest of the CMS site, we will continue to revisit these Theme principles and expand the contents of them, so it is good to keep their functionality in mind as we go along.

LEAVE A REPLY

Please enter your comment!
Please enter your name here