Sams Teach Yourself ASP.NET 2.0 in 24 Hours, Complete Starter Kit
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.
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"
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!).
|
Категории