Welcome!

.NET Authors: Liz McMillan, Peter Silva, Yakov Werde, Matthew Pollicove , Kevin Benedict

Related Topics: .NET

.NET: Article

Introducing ASP 2.0 Master Pages

Factor out the common page elements in your site

When the page creates its master property, any public properties are automatically typed and available within the content page. This means that we can directly access our MenuItem property use without having to cast a reference (as shown below).

lblMenu.Text = Master.MenuItem

The FindControl method is used to locate specific controls on the master page, as shown in Figure 9. By default the controls on a master page aren't directly accessible by content pages, because they are considered protected. However, by using the FindControl method you can locate and set the value of the label control located on the master page as shown in the code example below.


Protected Sub Button1_Click(ByVal sender As Object,
ByVal e As System.EventArgs) Handles Button1.Click
Dim mpLabel As Label
mpLabel = CType(Master.FindControl("Label1"), Label)
mpLabel.Text = "Click from the Master Page"
End Sub
It is important to keep in mind that at run time the ContentPlace-Holder control has been merged with content from a Content control. By default, the ContentPlaceHolder control will not contain its default content. Instead, it will contain the text and controls that are defined in the content page.

Up to this point we have declared master pages declaratively. It is possible to attach to a master page to a content page dynamically. The merging of the master page and the content page is done during the initialization stage of the page processing. In order to dynamically assign a master page, this has to be done prior to the page initialization. The only place this can be done and not generate an error is during the PreInit stage. This is the earliest event that occurs in an ASP.NET page life cycle. This can be done using the following code:


Sub Page_PreInit(ByVal sender As Object, ByVal e As EventArgs) _
Handles Me.PreInit
Me.MasterPageFile = "~/root.master"
End Sub
Site Branding with Master Pages
Up to this point, we have used the MasterPageFile attribute in the Page directive within content pages to declaratively specify the master. Even though this approach works, it does requires that each page directly add the MasterPageFile attribute. For larger sites, this probably isn't a practical solution. ASP.NET 2.0 provides a page element in the web.config that can automatically enforce this requirement. Essentially, all of the pages in that Web application will automatically use the designated master page. For example, the following web.config file entry specifies that all of the pages in the Web application should use Company.master as the default master page.

<configuration>
<system.Web>
<pages masterPageFile ="Company.master" />
</system.Web>
</configuration>
Specifying a master page within the web.config is merely setting a default. Even when this method is used to specify the name of a default master page, it can still be overridden using the Page-level directive. Any values specified within the Page directive automatically take precedence over the web.config file entry.

As the meeting started to wind down I could see a lot of relieved and smiling faces. The developers started talking among themselves as we wandered out into the hall. The lead developer walked with me towards my car with a pleasantly surprised smile on his face. He explained that he was glad to understand the basics of master. Of course, like this article, it's only the beginning and the rest is up to you to discover and implement within your Web sites.

More Stories By Thom Robbins

Thom Robbins is a senior technology specialist with Microsoft. He is a frequent contributor to various magazines, including .NET Developer's Journal and SOA Web Services Journal. Thom is also a frequent speaker at a variety of events that include VS Live and others. When he's not writing code and helping customers, he spends his time with his wife at their home in New Hampshire.

Comments (1) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
James Gordon 09/14/05 11:09:24 AM EDT

The same magazine had a much better article on Master pages in December 2005.This is pretty much a repeat