|
Comments
|
Today's Top SOA Links
ASP.NET Introducing ASP 2.0 Master Pages
Factor out the common page elements in your site
By: Thom Robbins
Sep. 9, 2005 03:00 PM
While meeting with the development staff of a local company we began discussing a project that the CIO had assigned them earlier that morning. The project was actually the first in a series of about 15 planned Web sites scheduled over the next year. Each site was designed to expose sales and marketing collateral to their international reseller community. Their lead developer explained that the first site would contain about a hundred pages, but when complete they expected each site to have several thousand distinct pages. He said, "Layout control and reusability are key for this project to have any chance of success." In their hasty research that morning they had run across a new feature of Visual Studio 2005 called master pages. It seemed to them that this feature might be an important key to this project. However, they hadn't been able to completely understand how it could be used. In this article, as I did that afternoon, we will cover how master pages provide a flexible, template-based architecture for building Web-based applications using Visual Studio 2005 and the .NET Framework 2.0.
The Anatomy of a Master Page Fundamentally, a master page is simply an ASP.NET file that has an extension of .master. Master pages are added to an existing Web site as a new project item, as shown in Figure 1. The master page is responsible for defining page layout, which can include things like static text, HTML elements, and server controls. The master pages also contain the top-level HTML elements for a page, such as <html>, <head>, and <form> tags. Once added to a project the master page contains a special @Master directive that replaces the standard @Page directive used in ordinary ASP.NET pages. The new directive looks like the following: <%@ Master Language="VB" CodeFile="MasterPage.master.vb" Inherits="MasterPage" %> By default a master page is designed to contain the common appearance and behavior for all linked content pages. Within a master page you add placeholders called ContentPlaceHolders as shown in Figure 2. This is where at run time the child pages will insert their rendered page output. The ContentPlaceHolder control is rendered using the following markup: Any page that uses a master page to define the layout may only place content within the areas defined by the ContentPlaceHolder. From a page designer's perspective, this ensures a consistent site design. Any attempt to place controls outside of the ContentPlaceHolder control on a linked content page will generate an ASP.NET error.
Implementing a Content Page For example, when adding a new ASP.NET Web form into a project that contains a master page, the item dialog contains an option to select a master page as shown in Figure 3. When selected, this box enables the Select Master Page dialog box as shown in Figure 4. Basically, a content page is simply a Web form with an enhanced page directive that causes the inheritance from a master page. Any existing Web forms can also derive from a master page by placing the MasterPageFile attribute in the page directive as shown below. <%@ Page MasterPageFile="root.master" %> In each of the content pages all page output is defined within the bounds of a Content control. Keep in mind that that the ContentPlaceHolder control is used to define a relative region within a master page. However, within a content page the Content control acts as the container for page information. When rendered on a content page this control uses the following markup. <asp:Content ID="Content1" ContentPlaceHolderID="MenuHolder" Runat="Server"> Reader Feedback: Page 1 of 1
Your Feedback
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
||||||||||||||||||||||||||||||