O'Reilly logo

ASP.NET 2.0: A Developer's Notebook by Wei-Meng Lee

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Modify a Master Page at Runtime

Note

You can flexibly modify the content of the Master page so that it suits the theme of the page.

When a Web Form that uses a Master page is loaded at runtime, it displays the content of the Master page together with its own content. However, there are times when you will want to modify parts of the Master page when a particular web page is loaded. For example, at the O'Reilly Network site (http://www.oreillynet.com), pages that belong to the ONDotnet subsite display the ONDotnet logo (a leaping dolphin) in their headers, rather than the generic O'Reilly Network logo (the ever-familiar tarsier). Pages at other subsites, such as Perl.com and ONLamp.com, do likewise.

How do I do that?

To see how a Master page can be modified on the fly, we'll add a page to our previous project (see Section 2.2). This page will use MasterPage.master and change the image in the Master page from the O'Reilly Network logo to the ONDotnet logo when it is loaded.

  1. Using the project created in the previous lab, add a new Web Form (right-click the project name in Solution Explorer, select Add New Item..., and then select Web Form) and check the "Select master page" checkbox at the bottom of the dialog. Name the Web Form ONDotnet.aspx. Click Add.

  2. Select Masterpage.master as the Master page.

  3. Populate the ONDotnet.aspx Web Form with text (see Figure 2-14).

    The ONDotnet.aspx Web Form with text added

    Figure 2-14. The ONDotnet.aspx ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required