Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Trial Issue of Visual Studio Magazine

email article
printer friendly
get the code
more resources

Create Master Pages in ASP.NET 2.0
ASP.NET 2.0's master pages can help you develop consistent Web apps without the additional overhead of include files, user controls, or third-party add-ins.
by Doug Thews

December 22, 2004

Technology Toolbox: VB.NET, ASP.NET

The implementation of master pages is a highly anticipated feature in ASP.NET 2.0. A master page is a template that you can use to define the overall look and feel of your ASP.NET Web applications. In previous versions of ASP.NET, you were forced to embed user controls in every page to get consistency. Master pages now make it much easier to deliver a consistent look and feel, without the need to embed user controls across all of your pages.

ADVERTISEMENT

I'll discuss the basics of ASP.NET 2.0 master pages, how to create and use them, and how ASP.NET uses them to render a final Web page, complete with postback events. The code for this article is based on VS.NET 2005 beta 1, and as with all beta versions, there is the possibility that some features shown in this article might change before the final release of VS.NET 2005.

A master page is basically a template where you can define the style and functionality of your Web application. You define a master page that contains how you want things to look and act, and then you create what are called content pages (that refer to the master page) for all of the pages in your Web application.

Create a master page by opening up a VS.NET 2005 Web project and selecting Add | New Item from the menu (see Figure 1). It's probably a good idea to place your master pages in a subdirectory within your Web application in order to improve the manageability of the Web project (I used the MasterPages folder in the source code example for this article). Notice that the default master page contains a new control called <asp:contentplaceholder> between an HTML <DIV> tag (see Listing 1). You use the <asp:contentplaceholder> to define the area that content pages will place their content in. Outside the <asp:contentplaceholder> is where you define things such as headers, footers, menus, and styles that you want to implement consistently across your Web application. Adding your HTML (or ASP.NET controls) outside of the <asp:contentplaceholder> is simply a matter of formatting to taste, so I won't waste article space with it here. Suffice it to say that you have the wealth of HTML, CSS, and ASP.NET user and server controls at your disposal. A master page can also have multiple <asp:contentplaceholder> controls.

After creating the master page, notice that it looks like any other page, but it uses a new directive called <%@ Master %>:

<%@ Master Language="VB" 
   CompileWith="MasterPage.master.vb"
   AutoEventWireup="false" 
      ClassName="MasterPage_master" %>

You can also place default content within the ContentPlaceHolder:

<asp:contentplaceholder 
   id="BasicContentPlaceHolder" 
   runat="server">
   No default content specified
</asp:contentplaceholder>

This code specifies that a text string will be rendered in the placeholder's position when a page that inherits this master page does not specify any content to be placed within the <asp:contentplaceholder>.

It's All Relative
One trick you'll need to learn when working with master pages revolves around relative addressing. Let's say you place an image in the header of a master page, and all images are in a separate Images directory:

<img src="../Images/AG00004_.GIF"  />



Back to top














Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home