| By Thom Robbins | Article Rating: |
|
| September 9, 2005 03:00 PM EDT | Reads: |
31,048 |
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.
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.
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
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:
Site Branding with Master Pages
Sub Page_PreInit(ByVal sender As Object, ByVal e As EventArgs) _
Handles Me.PreInit
Me.MasterPageFile = "~/root.master"
End Sub
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.
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.
<configuration>
<system.Web>
<pages masterPageFile ="Company.master" />
</system.Web>
</configuration>
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.
Published September 9, 2005 Reads 31,048
Copyright © 2005 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
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.
![]() |
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 |
||||
- Kindle 2 vs Nook
- Confessions of a Ulitzer Addict
- IBM Hardware Chief, Intel VC Exec Arrested in Insider Trading Scam
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- Infrastructure-as-a-Service Will Mature in 2010: Microsoft's David Chou
- Windows 7 – Microsoft’s First Step to the Cloud
- Cloud Expo and the End of Tech Recession
- Jill Tummler Singer, Deputy CIO of CIA, Keynotes at GovIT Expo
- Reality Check at the Cloud Computing Expo
- Visual Studio 2010 Is Cloud Friendly
- Fired SCO CEO Fires Back
- Kindle 2 vs Nook
- The Difference Between Web Hosting and Cloud Computing
- Ajax in RichFaces 3.3, JSF 2 and RichFaces 4
- Confessions of a Ulitzer Addict
- Wave on Ulitzer: Confessions of a Google Wave Fanboy
- IBM Hardware Chief, Intel VC Exec Arrested in Insider Trading Scam
- Cloud Computing Best Practices
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- Infrastructure-as-a-Service Will Mature in 2010: Microsoft's David Chou
- Eval JavaScript in a Global Context
- Windows 7 – Microsoft’s First Step to the Cloud
- Google Maps and ASP.NET
- Crystal Reports XI & How It Has Changed
- Converting VB6 to VB.NET, Part I
- Creating Controls for.NET Compact Framework in Visual Studio 2005
- Where Are RIA Technologies Headed in 2008?
- How to Write High-Performance C# Code
- AJAX World RIA Conference & Expo Kicks Off in New York City
- Implementing Tab Navigation with ASP.NET 2.0
- i-Technology Photo Exclusive: Bill Gates & Steve Jobs In "Nerds"
- .NET Archives: Getting Reacquainted with the Father of C#
- i-Technology Viewpoint: "SOA Sucks"
- Programmatically Posting Data to ASP .NET Web Applications




























