Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [172]

By Root 5417 0
a content page to a Visual Studio project.

Figure 8-1. Adding a content page to a Visual Studio project.

The next step entails choosing a particular master page from within the folders of the current project. Normally, master pages are located in the root folder that defines their scope. If you have only one master page, it is usually located in the root of the site.

The Content Control


The key part of a content page is the Content control—a mere container for other controls. The Content control is used only in conjunction with a corresponding ContentPlaceHolder and is not a standalone control. The master file that we considered earlier defines a single placeholder named PageBody. This placeholder represents the body of the page and is placed right below an HTML table that provides the page’s header. Figure 8-2 shows a sample content page based on the aforementioned master page.

Figure 8-2. A preview of the content page in Visual Studio 2010.

Let’s take a look at the source code of the content page:

<%@ Page Title="Home Page"

Language="C#"

AutoEventWireup="true"

MasterPageFile="~/Site.master"

CodeBehind="Default.aspx.cs"

Inherits="UserProfileDemo._Default" %>

Page personalization

The content page is the resource that users invoke through the browser. When the user points her or his browser to this page, the output in Figure 8-3 is shown.

The replaceable part of the master is filled with the corresponding content section defined in the derived pages.

Figure 8-3. The sample page in action.

Content Pages and Auxiliary Content


A content page—that is, a page bound to a master—is a special breed of page in that it can only contain controls. A content page is not permitted to host server controls outside of an tag.

As a collection of tags, a content page is not even allowed to include any markup that specifies general information such as the title of the page, inline styles, and even scripts.

You can declaratively set the title of a content page using the Title attribute of the @Page directive as shown here:

<@Page MasterPageFile="site.master" Title="Hello, world" %>

However, there’s not much you can do to add styles and scripts in a content page unless the master page provides for specific placeholders. You can add styles and scripts to a placeholder if the placeholder’s position in the layout allows you to include them. Most of the time, you create a placeholder within the section and perhaps another at the bottom of the page to allow for styles and scripts. The default master you get in sample Visual Studio 2010 projects has the following:

...

The HeadContent placeholder just exists so that content pages can fill it with any page head–specific content such as script or styles. Likewise, you can create a script-only placeholder and place it at the bottom of the page to improve the page’s rendering speed, as discussed in Chapter 7.

Note, though, that a placeholder is just a container you can fill with whatever ends up producing valid HTML markup. You have no way to restrict a placeholder to contain only certain controls or certain fragments of HTML markup. Later in the chapter, I’ll return to this point, contrasting placeholders with master page properties.

For now let’s explore in a bit more detail the techniques to attach pages to masters.

Attaching Pages to a Master


So far, we have bound any content page to its master by using the MasterPageFile attribute in the @Page directive. The MasterPageFile

Return Main Page Previous Page Next Page

®Online Book Reader