Comments
L W wrote: Dear Sir, Please do forward a Google Wave Invitation to lvw.iv4 (at) gmail (dot) com, at your earliest convenience? Much appreciated!
Cloud Computing
Conference & Expo
November 2-4, 2009 NYC
Register Today and SAVE !..

SYS-CON.TV
Today's Top SOA Links


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.

About 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.

In order to post a comment you need to be registered and logged in.

Register | Sign-in

Reader Feedback: Page 1 of 1

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


Your Feedback
James Gordon wrote: The same magazine had a much better article on Master pages in December 2005.This is pretty much a repeat
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

SYS-CON Featured Whitepapers
ADS BY GOOGLE