Datasheet
Listing 1-1: (continued)
<title>Master Page</title>
</head>
<body>
<form runat=”server”>
Master Page Content
<br/>
<b>
<asp:ContentPlaceHolder id=”MiddleContent” runat=”server”>
</asp:ContentPlaceHolder>
</b>
</form>
</body>
</html>
Apart from looking at the file extension, you can also identify a master file by looking at the new page
directive named
master at the top of the page. This declarative is used to identify that the current page
is a master page and prevents users from requesting the page from a browser. Inside the code, the code
contains an element named
asp:ContentPlaceHolder, which will be used by all the content pages to
render appropriate content that is specific to their pages. That’s all there is to creating the master page.
To create a content page, add a new ASP.NET page named
ContentPage.aspx and modify the code as
follows:
<%@ page language=”c#” MasterPageFile=”~/CommonPage.master” %>
<asp:Content id=”Content1” ContentPlaceHolderID=”MiddleContent”
runat=”server”>
Child Page Content
</asp:Content>
The code required for the content page is very simple and straightforward. As part of the page directive,
specify a new attribute named
MasterPageFile that is used to identify the name of the master page
that you want to utilize. This example uses the master page created in Listing 1-1. Next, you have a new
element named
asp:Content that is used to associate the asp:ContentPlaceHolder element in the
master page with the content page. This is done through the use of the
ContentPlaceHolderID
attribute. That’s all that is there to creating a master page and using the master page from a content
page. Now, if you request the content page from a browser, you will get the output that is produced by
merging the master page with the content page.
New Code-Behind Model in ASP.NET 2.0
ASP.NET 1.x supports two coding models: the inline model, in which markup and code coexist in the
same ASPX file, and the code-behind model, which places markup in ASPX files and code in source code
files. ASP.NET 2.0 introduces a third model: a new form of code-behind that relies on the new partial
classes support in the Visual C# and Visual Basic compilers. Code-behind in ASP.NET 2.0 fixes a nagging
problem with version 1.x: the requirement that code-behind classes contain protected fields whose types
and names map to controls declared in the ASPX file.
The following code listing shows the
.aspx file named HelloWorld.aspx:
<%@ Page Language=”C#” CodeFile=”HelloWorld.aspx.cs” Inherits=”HelloWorld” %>
<html>
<body>
6
Part I: Introduction
05_041796 ch01.qxp 12/29/06 9:09 PM Page 6