10.4. Using Master Pages with Slavish Devotion

Chances are, most of the Web pages in your site have something in common. For example, they might use the same banner, menu, login link, background pattern, and copyright blurb. Master pages in ASP.NET let you collect those common chunks in one central file instead of duplicating the code in every page.

Master pages include placeholder areas where you plug in your regular, changeable page content. At runtime, ASP.NET merges all the bits for you.

10.4.1. Creating a master page

Working with a master page is very similar to designing and programming a regular Web page. You can drag and drop controls and work with events. Follow these steps to create a master page:

  1. In Solution Explorer, right-click the project name and, from the context menu, choose Add New Item.

  2. In the Templates area, select Master Page.

    NOTE

    The sorting of the templates in Add New Item puts Master Page before AJAX Master Page.

  3. Open the master page in Source view, drag an Image control from the Toolbox and drop the control just after the default <div> tag and before <asp:ContentPlaceHolder>.

NOTE

Although you can use Design view to drop controls into a master page, it's easy to inadvertently put master content inside the <asp:ContentPlaceHolder> tags. If you find master content in the wrong place, you can move it where it belongs and grumble about why there's no warning.

Figure 10-7 shows part of a master page in Design view. In the lower half of the figure are two ...

Get ASP.NET 3.5 For Dummies® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.