Recipe 1.3. Extending a Master Page's Content to Include Content for Other Application PagesProblemYou have a master page that provides the basic content for some of your pages but you would like to extend the master page to include additional content for other pages. You might want to do this, for example, when you want the login page of your application to appear one way and the pages that follow it to appear in another way that builds on the appearance of the login page. SolutionNest master pages as outlined here. Create the base master page that contains the minimum content used by content pages and then create the content pages that require the content of the base master page. Next, create another master page that references the base master page and adds additional content required for other pages. Now, create content pages that require the content of master pages and reference the second master page. In the .master file of the base master page:
In the .aspx file of the pages that will use the base master page:
In the .master file of the second master page:
In the .aspx file of the pages that will use the second master page:
Figure 1-2 shows the output of the page that uses the nested master page in our example. The base master page and a sample content page that uses the master page are shown in Examples 1-1 and 1-2 in Recipe 1.1. Example 1-3 shows the .master file, and Example 1-4 shows the .aspx file of this example. Figure 1-2. Nested master page example outputDiscussionIn many web applications, the content of the pages is a function of the location within the application. A login page, for example, may contain only a header and the required controls for inputting the login credentials and a button to initiate the login. Pages displayed after login may contain the same header as the login page plus navigational sections under the header and potentially along the left side of the pages. You can use master pages to support this structure and define the HTML only once. In this example, we use the master page defined in Recipe 1.1 as the base master page for nesting. It contains a header that will be used for all pages. In the second master page, we have added a menu to the left side of the page and another asp:ContentPlaceHolder for the page-specific content that will be to the right of the menu. Figure 1-3 shows the hierarchy of the nested master pages in this example. Figure 1-3. Hierarchy of nested master pagesContent pages that need only a header will reference the base master page (ASPNet-CookbookVB.master) and provide the content for the PageBody asp:ContentPlace-Holder control. Content pages that need a header and the left menu will reference the second master page (CH01NestedMasterPageVB.master) and will provide content for the ContentBodyasp:ContentPlaceHolder control.
Master pages can be as simple or complex as your application dictates. They can include any number of content placeholders and can be nested to any level that meets the needs of your application. See AlsoRecipe 1.1 Example 1-3. Nested master page (.master)
Example 1-4. Nested master page (.aspx)
|