Providing Default Content in a Master Page


The ContentPlaceHolder controls in a master page represent the portions of the template that are editable on a page-by-page basis. Typically, an ASP.NET page that inherits the master page will customize these regions. However, in some circumstances a page may not want to customize a particular ContentPlaceHolder region, instead falling back on some default value specified by the master page.

For example, imagine that instead of having the title Welcome to My Website! displayed on each page in the header region, you wanted to allow each page to customize this title. However, you suspect that the majority of the pages will just be interested in using the value Welcome to My Website! To accomplish this, you could add a ContentPlaceHolder control in the header region and specify the default value of the ContentPlaceHolder as the title Welcome to My Website! Then, in the ASP.NET pages that inherited this master page, you could indicate whether the page should use the master page's default content or specify its own customized content.

To illustrate creating default value in a master page's ContentPlaceHolder control, let's create a new master page called DefaultExample.master that has the same layout as the MasterPage.master master page we created earlier.

Did you Know?

To quickly copy the layout from MasterPage.master to DefaultExample.master, go to the Design view of MasterPage.master. Then select all content by going to the Edit menu and choosing Select All. After selecting all, go to the Edit menu and choose Copy. Return to DefaultExample.master's Design view and select all content in this page and then go to the Edit menu and choose Paste.

Voila! You've just copied the content from one master page to another.


In the DefaultExample.master page, add a second ContentPlaceHolder control to the header region, removing the text Welcome to My Website! Set this ContentPlaceHolder control's ID property to HeaderRegion. To specify a default value for the HeaderRegion ContentPlaceHolder, simply add the default content to the ContentPlaceHolder, much like you would on an ASP.NET page to customize a particular content region. Because we want the default title to still be Welcome to My Website!, type this text into the ContentPlaceHolder in the header region. That's all there is to it!

Choosing to Use Default or Custom Content in an ASP.NET Page

An ASP.NET page that inherits from a master page can opt to provide custom content for a master page region or, instead, rely on the master page's default content for that region. To illustrate this, take a moment to add a new ASP.NET page that inherits from the DefaultExample.master page we just created. Because the master page has two ContentPlaceHolder controls in the Design view of the ASP.NET page, you'll see two content regions; similarly, the ASP.NET page's declarative syntax has, by default, two Content controls:

[View full width]

<%@ Page Language="VB" MasterPageFile="~/DefaultExample.master" AutoEventWireup="false" CodeFile="DefaultContentTest.aspx.vb" Inherits="DefaultContentTest" title="Untitled Page" %> <asp:Content ContentPlaceHolder Runat="Server"> </asp:Content> <asp:Content ContentPlaceHolder Runat="Server"> </asp:Content>


To use the master page's default content for a region, rather than custom content, you need to remove the associated Content Web control from the ASP.NET page. To use the default content for the header region, simply delete the first Content control, the one whose ContentPlaceHolderID equals HeaderRegion. You can accomplish this by manually removing the Web control syntax from the Source view or by going to the Design view, right-clicking on the editable region, and choosing the Default to Master's Content option. Once this Content Web control has been removed from the ASP.NET page, the page will use the default content from the master page (Welcome to My Website!).

By the Way

To stop using the master page's default content and to create custom content for the page, you need to add back the Content Web control. You can do this manually, through the Source view, or by going to the Design view, right-clicking on the content region, and selecting Create Custom Content.





Sams Teach Yourself ASP. NET 2.0 in 24 Hours, Complete Starter Kit
Sams Teach Yourself ASP.NET 2.0 in 24 Hours, Complete Starter Kit
ISBN: 0672327384
EAN: 2147483647
Year: 2004
Pages: 233

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net