Browse DevX
Sign up for e-mail newsletters from DevX


Introducing ASP.NET 2.0 Master Pages : Page 3

Building individual Web pages is easy. Building Web pages that adhere to specific site and application guidelines is much more difficult. Microsoft has come to the rescue with Master Pages, which builds page template capability directly into ASP.NET 2.0. This article shows you how to use the new Master Pages feature to create consistent page layouts for your Web applications.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Nesting Master Pages
There are times where you may want to provide overall branding and appearance for your Web site, but at the same time provide sub-sites whose appearance must be consistent. To accomplish this, you need to nest one master page within another page, a process called "Nesting Master Pages". Nesting support is built-in, which means you don't have to do anything special to reap the advantages of nested master pages; a content page that uses a sub-master page will automatically receive content from all the master pages in the hierarchy.

Consider the MSDN Web site as an example to understand how you can use the nested master page architecture to provide a consistent look and feel. As you probably know, MSDN is the home for several developer centers that cover the .NET framework, ASP.NET, Visual Studio, Security, Web services, and Windows Server 2003, among others. Using the master pages architecture, you could implement a consistent look and feel across all the developer centers. For example, you can have the main MSDN page derive from a root master page. Each developer center can have its own master page, each of which uses the root master page as its master. This means a content page in any of the developer centers will inherit the root master page look and feel settings that provide overall MSDN branding, and will also inherit the custom look and feel specific to that developer center. This nested approach provides each developer center with the freedom to develop customized content while maintaining a consistent overall branding for the entire site. The downloadable code for this article provides an example of nested master pages.

Configuring Master Pages
So far, you've used the master attribute of the Page directive in the content pages to specify the name of the master page. Even though this approach works, it requires you or your developers to specify the master attribute in each page. You can eliminate that requirement by specifying the name of the master file in the web.config file under the pages element. After adding the entry to your web.config file, all the pages in that Web application will automatically use the designated master page. For example, the following web.config file entry specifies that all the pages in the Web application should use IntroMaster.master as their default master page.

<configuration> <system.Web> <pages master="IntroMaster.master" /> </system.Web> </configuration>

You're not limited by setting the default master page in the web.config file. Even when you use this method to specify the name of a default master page, you can still override the global value by specifying a different master page in the master attribute of the Page directive. Any values you specify using the Page directive takes precedence over the web.config file entry.

To sum up, ASP.NET's new Master Pages feature provides a clean approach to encapsulate common functionality and content a centralized location, allowing other pages to inherit from those master pages, thereby greatly reducing the maintenance. Using master pages, you can also create one set of controls and code and apply the results to all your content pages. Apart from providing a fine-grained control over the layout of the content pages, master pages also expose an object model that allows you to access the master page controls, properties and methods from content pages.

Thiru Thangarathinam works at Intel Corporation in Chandler, Arizona. He specializes in architecting, designing, and developing distributed enterprise class applications using .NET related technologies. He has co-authored a number of books in .NET related technologies and is a frequent contributor to leading technology related online publications. He can be reached .
Thanks for your registration, follow us on our social networks to keep up-to-date