oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Master Pages in ASP.NET
Pages: 1, 2

To use the sub-master, let's create two additional .aspx pages (OnDotNetPage1.aspx and OnDotNetPage2.aspx). In the first, add an <asp:Content> block that adds text to the page. In the second, leave the block out entirely, thus using the default text provided by the OnDotNet.master page. The results are shown in Figure 7:

Figure 7. The sub-master page

Each of these pages is divided into three areas. The topmost area has the O'Reilly logos from TopMasterPage.master. The middle area has the logo from the sub-master page OnDotNet.master. Finally, each of the .aspx pages has content. In OnDotNetPage1.aspx, you see the custom content you added taking the place of the default text. Since OnDotNetPage1.aspx did not add content, it inherits the default content from OnDotNet.master.

You can have multiple sub-master pages that all share a common master page. For example, you might create a second sub-master named Liberty.Master, as shown in Figure 8:

Figure 8. Adding a second sub-master page

This new sub-master uses a different image, and adds text (shown circled in red). You could, of course, make significant differences in each of your sub-master pages (e.g., one might add a layer of menus, another might add a site map, and so forth).

To see your new sub-master at work, create three new pages: LibertyOnWhidbeyPage1.aspx, LibertyOnWhidbeyPage2.aspx, and LibertyOnWhidbeyPage3.aspx. All three of these new pages will share the top master page with the OnDotNet pages, but these new pages will have their own middle section created by the Liberty.master sub-page, as shown in Figure 9:

Figure 9. All of the master pages

Dynamically Editing the Master

You may decide that in response to certain events, you'd like to reach up into the master page (from an .aspx page) and change its presentation. This can be done by exposing a property in the master page and then using the master member of your .aspx page to access that property.

Note: You can also accomplish this task with late-binding using the FindControl() method, but that uses reflection and is considerably slower.

For example, you might add a property to your LibertyMaster.master page that gets and sets the URL for the libertyImage image file shown declared in Figure 8 (above). The actual code is written into the code behind page (Liberty.master.cs), and might look like this:

public partial class Liberty_master
   public Image LibertyImage
      get { return this.libertyImage; }
      set { this.libertyImage = value; }


Once you have that property declared in the master page, you can access it in the .aspx page, as follows:

public partial class LibertyOnWhidbeyPage3_aspx
  public void Page_Load( object sender, EventArgs e )
    this.Master.LibertyImage.ImageUrl = "~//dotnet/2004/09/27/graphics/Triangle.jpg";

The result is that when the page LibertyOnWhidbeyPage3.aspx loads, the image normally used in the Liberty.master is changed for this specific page, as shown in Figure 10:

Figure 10. Dynamically edited masters

As you can see, while LibertyOnWhidbeyPage1.aspx uses the image supplied by LibertyMaster.master, LibertyOnWhidbeyPage3.aspx actually changes that image in the master page, before displaying. This allows you to have your cake and eat it, too; that is you can modify your template when needed, but use the template as designed for most pages, to create a consistent user interface.


Jesse Liberty is a senior program manager for Microsoft Silverlight where he is responsible for the creation of tutorials, videos and other content to facilitate the learning and use of Silverlight. Jesse is well known in the industry in part because of his many bestselling books, including O'Reilly Media's Programming .NET 3.5, Programming C# 3.0, Learning ASP.NET with AJAX and the soon to be published Programming Silverlight.

Return to