
Combining Site Navigation with Master Pages
In the pages that you have created up to this point in this walkthrough, you have added site navigation controls individually to each page. Doing this is not especially complex, because you do not have to configure the controls differently for each page. However, it can add to the maintenance costs for your site. For example, to change the location of the SiteMapPath control for pages in your site, you would have to change each page individually.
By using site navigation controls in combination with master pages, you can create a layout that contains the navigation controls in one location. You can then display pages as content within the master page.
To create the master page for navigation
In Solution Explorer, right-click the name of the Web site, and then click Add New Item.
In the Add New Item <Path> dialog box:
Under Visual Studio installed templates, click Master Page.
In the Name box, type Navigation.master, and then click Add.
Switch to Design view.
The master page appears with a default ContentPlaceHolder control.
In the following procedure, you will create a master page with a simple layout for navigation controls. In a real application, you would likely use more sophisticated formatting, but the techniques for using navigation controls on a master page will be similar.
To add navigation controls to the master page
Select the ContentPlaceHolder control, press LEFT ARROW, and then press ENTER.
This inserts a blank line in front of the ContentPlaceHolder control.
From the Data group in the Toolbox, drag a SiteMapDataSource control onto the master page and position it above the ContentPlaceHolder control.
Note: |
|---|
Do not position the
SiteMapDataSource control on the ContentPlaceHolder control.
|
By default, the SiteMapDataSource control will use the Web.sitemap file that you created in "Creating a Site Map," earlier in this walkthrough.
Click the SiteMapDataSource control, press RIGHT ARROW, and then press ENTER.
This inserts a blank line under the SiteMapDataSource control.
On the Table menu, click Insert Table, and then insert a table that has one row, two columns, and a width of 100 percent.
From the Navigation group in the Toolbox, drag a TreeView control onto the left cell of the table.
On the TreeView Tasks menu, in the Choose Data Source box, click SiteMapDataSource1.
From the Navigation group in the Toolbox, drag a SiteMapPath control onto the right cell of the table.
In the right cell, click the blank area, and then press SHIFT+ENTER to create a new line.
Drag the ContentPlaceholder control onto the right cell of the table and position it under the SiteMapPath control.
When you are using a master page, you create the pages in your site as content pages. The content pages use Content controls to define the text and controls that are displayed in the ContentPlaceholder control of the master page. Therefore, you will have to re-create the Home, Products, and Hardware pages as content pages.
To create content pages for the site
In Solution Explorer, right-click the Home.aspx page, click Delete, and then click OK.
Repeat step 1 for the Products.aspx, Software.aspx, Training.aspx, and Hardware.aspx pages, and any other pages you have created.
You will re-create the pages as content pages that use a master page.
In Solution Explorer, right-click the name of the Web site, and then click Add New Item.
In the Add New Item dialog box:
Under Visual Studio installed templates, click Web Form.
In the Name box, type Home.aspx.
Select the Select master page check box.
Click Add.
The Select a Master Page dialog box appears.
Under Contents of folder, click Navigation.master, and then click OK.
You have created a content page that is bound to the master page that you created in the preceding section.
Switch to Design view.
In Design view, you see the layout that you created for the master page, with an editable region corresponding to the ContentPlaceHolder1 control on the master page.
Click inside the Content window.
This is where you can add content for this specific page.
Type Home, and then format the text as Heading 1.
You have created the static text (specifically, the heading) for the Home page.
Repeat steps 3 through 8 to create a Products.aspx content page and a Hardware.aspx content page. In step 8, type Products and Hardware, respectively, instead of Home.
Testing Navigation Controls in the Master Page
Testing with master pages and content pages is the same as testing individual pages.
To test navigation controls in the master page
Open the Products.aspx page, and then press CTRL+F5 to run the page.
The Products content page is merged with the master page. In the browser, you see a page that contains the Products heading and the navigation controls that you added to the master page.
In the TreeView control, click Hardware.
The Hardware page is displayed with the same layout as the Products page, except that the SiteMapPath control displays a different path.