Advertisment

A Common Look with Master Pages

author-image
PCQ Bureau
New Update

ASP (Active Server Pages) made creating dynamic and database-driven Web pages a very simple task. Similarly, ASP.NET redefined the way that you built Web pages. Moving from a page-centric, scripting model of ASP to an application-centric, framework model with a fully functional and powerful RAD tool of ASP.NET (Visual Studio.NET) that let developers create Web-based, enterprise-level applications as easily as they would have been able to create Windows applications.

Advertisment

The upcoming release of ASP.NET, version 2.0, codenamed as Whidbey, has a number of exciting features. We’ll look at one of these, called Master Pages. Master Pages in ASP.NET 2.0 are templates that you can use in the Web solution. These are, however, much more powerful than any template that you can create with, say, FrontPage or DreamWeaver. Let’s take a look at how they work and the code that goes into creating them. 

Snapshot
Applies to Web developers
Usp Easier way to a consistent look and feel

When creating a large Web portal or an enterprise intranet application, there are many cases where you might require a consistent look and feel across the pages as well as some common functionality such as navigation or dynamic content. Of course, a developer can currently achieve this by using, include files or complex templates in Web page-building tools as mentioned above. The disadvantage of this approach is that the template is decided and fixed at the time of the design itself and modifications after deployment are complex. 

Advertisment

ASP.NET 2.0 has the concept of Master Pages that you can create in VS.NET Whidbey. This lets you create much more dynamic, powerful and easy-to-modify Web pages based on the template. For instance, if you have a Master Page called ‘Site.Master’ (.master being the new file extension for Master Pages), you can easily create content pages that use this as their master template. Each of them will run and use the master file for the common content first and then add its own content in special areas. These areas are called ContentPlaceHolder’s. See the block diagram on the following page to know what these mean and how they work.

Creating a Master Page



To create a Master Page in VS.NET Whidbey (VSW), all you need to do is to select Add New Item>Master Page in the IDE, which creates a .master file. This master file is nothing but an ASPX page itself with certain additional features. The code to create the master file looks as the following code.

In this page, there are two important things to note. The first is the ‘Master’ declaration in the first line of the page. This specifies that the page is a master and cannot be retrieved directly from a browser. 

Advertisment
<%@ Master language=”VB” %>



























The second is the placement of the tag. This specifies that content pages that use this master can place all their content inside the area specified by this tag. There can, of course, be more than one such tag in a master to allow different areas that are usable by the content pages. The ID attribute binds the content in the placeholder. 

You can, of course, add other content to the Master Page itself. For instance, you can use a Master Page for ‘branding’ your company logo and tagline outside the placeholder. All content pages will then get this branding automatically but will be unable to change it. You can also place content within the placeholder, though this will only be a default content and can be overridden by the content page.

Advertisment

Using a Master Page



Now, to create a content page that uses a Master Page, select ‘New Content Page’ from the ‘Add New Item’ menu in VSW. This allows you to then select the Master Page that it uses as a template.

When viewed in the Code View of VSW, this is what you’ll see for the page. 






<%@ Page language=”VB” master=”Site.master” %>







This, of course, assumes that the master file is called Site.master. Now comes the interesting part. There is no mention of any reference to any placeholder. However, when you switch to Design mode, you’ll see the Master Page as well as the content placeholder. This is because VSW renders the Master Page but does not find any corresponding content for the placeholder, so it displays the placeholder instead. You can now add content by selecting Common Tasks>Create Empty Content, or by dropping controls into this area. The Code view reflects the change with this code.

























As you can see, the tag now gets associated with the placeholder in the Master Page by its ID.


Advanced stuff using Masters



Master Pages can also be nested. That is, a Master Page can use another Master Page as a template and the content page that uses this one will inherit the data from each of the nested Masters. This leads to interesting possibilities as one can create extremely generic Masters that can then be imported at a slightly lower level by other Master Pages and then used 



along with customized content in the template for that level.

Advertisment

Master Pages also have the ability to serve device-specific templates. Since ASP.NET 2.0 can detect more devices and browsers better than before, it allows you to create separate Master Pages for each device. To use these, you need to change the line in the content page as follows.






<%@ Page language=”VB” 


master=”default.master” 


Mozilla:master=”mozilla.master” %>




This specifies that if the browser is found to be Mozilla, use the Master Page called Mozilla.master. For all other browsers, use

default.master. 

As you can see, used in conjunction with VSW, Master Pages could help build large applications with consistent and easily modifiable templates easily. ASP.NET 2.0 has many more new and useful features. So keep your eye on this space for more.

Vinod Unny 



Enterprise InfoTech

Advertisment