Online Book Reader

Home Category

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

By Root 5301 0
dependent files are not touched.

Let’s expand on the previous example to add an intermediate master page. The root master page is the Site.master file we met earlier. The root master defines the header, the footer, and replaceable regions for the head and main content. Let’s add an intermediate master page to further structure the main content. The intermediate master page is named MainContent.master.

<%@ Master Language="C#"

AutoEventWireup="true"

MasterPageFile="~/Site.Master"

CodeBehind="MainContent.Master.cs"

Inherits="Masters.MainContentMaster" %>

How MainContent.Master replaces MainContent

What is this page for?

As you can see, the master contains both a collection of and tags. The top directive is that of a master, but it contains the MasterPageFile attribute, which typically characterizes a content page.

An intermediate master page is essentially a content page and must fulfill the rules of content pages such as not having markup outside controls. At the same time, it is allowed to specify the @Master directive and host some additional (well, nested) content placeholders.

Note that the final content page has access only to the placeholders of its immediate master. The HeadContent placeholder defined on the root master can be filled up by the intermediate master, but not by the final content page.

The following code illustrates nesteddemo.aspx—a content page that builds on two masters:

<%@ Page Title="Nested master pages"

Language="C#"

AutoEventWireup="true"

CodeBehind="NestedDemo.aspx.cs"

MasterPageFile="~/MainContent.Master"

Inherits="Masters.NestedDemo" %>

How NestedDemo.aspx replaces PageBody

[Your custom markup here]

Figure 8-6 shows the results.

Figure 8-6. The page results from the combination of two master pages.

At this point, if you create a new page from MainContent.Master you’ll be able to add custom content only below the label that says “What is this page for?”. Everything else is fixed and can’t be changed from the content page. Nested masters are fully supported by Visual Studio 2010, which provides you with a visual experience, as shown in Figure 8-7.

Figure 8-7. Nested masters in Visual Studio 2010.

What’s the purpose of having nested master pages?

Whereas a master page helps share a common layout through multiple pages, nested master pages simply give you more control over the structure of the final pages. Especially in sites with hundreds of pages, a single layout is not realistic. More likely, you need a super-template in which different areas are filled in a way for a bunch of pages and in another way for another bunch of pages. Each group of pages might be derived from an intermediate master.

When you create a content placeholder in a master page, you are leaving to the content page author full freedom to put in the placeholder wherever she wishes. Sometimes, instead, you want pages to customize the content of certain areas but without altering the layout. In Figure 8-6, the MainContent placeholder defined on the root master has been filled up as follows. (I omitted the fieldset you see in Figure 8-6 for clarity.)

What is this page for?

®Online Book Reader