</body>
</html>
Loading this page will produce the output we saw in Figure 4.10.
Now, this is a very simple example indeed, but we can easily extend it for other
purposes. You can see in the code snippet that we set the LabelText property
directly in the controls tag; we could have accessed the properties from our code
instead. Here's an example:
Visual Basic File: ControlTest.aspx (excerpt)
<script runat="server" language="VB">
Protected Sub Page_Load()
nameSb.LabelText = "Name:"
addressSb.LabelText = "Address:"
countrySb.LabelText = "Country:"
phoneSb.LabelText = "Phone:"
End Sub
</script>
C# File: ControlTest.aspx (excerpt)
<script runat="server" language="C#">
protected void Page_Load()
{
nameSb.LabelText = "Name:";
addressSb.LabelText = "Address:";
countrySb.LabelText = "Country:";
phoneSb.LabelText = "Phone:";
}
</script>
Master Pages
Master pages are a new feature of ASP.NET 2.0 that can make an important
difference in the way we compose web forms. Master pages are similar to web
user controls in that they are also composed of HTML and other controls; they
can be extended with the addition of events, methods, or properties; and they
cant be loaded directly by usersinstead, theyre used as building blocks to
design the structure of your web forms.
A master page is a page template that can be applied to give many web forms a
consistent appearance. For example, a master page can set out a standard structure
132
Chapter 4: Constructing ASP.NET Web Pages
containing the header, footer, and other elements that you expect to display in
multiple web forms within a web application.
Master page files have the .master extension, and, just like web forms and web
user controls, they support code-behind files. All master pages inherit from the
class System.Web.UI.MasterPage.
Designing a site structure using master pages and web user controls gives you the
power to easily modify and extend the site. If your site uses these features in a
well-planned way, it can be very easy to modify certain details in the layout or
functionality of your site, because updating a master page or a web user control
has immediate effects on all the web forms that use the file.
As weve already mentioned, a master page is built using HTML and controls,
including the special ContentPlaceHolder control. As its name suggests, the
ContentPlaceHolder is a placeholder that can be filled with content relevant to
the needs of each web form that uses the master page. In creating a master page,
we include all of the basic structure of future pages in the master page itself, in-
cluding the <html>, <head>, and <body> tags, and let the web forms specify the
content that appears in the placeholders.
Lets see how this works with a simple example. Suppose we have a site which
has many pages that contain a standard header, footer, and navigation menu,
laid out as per the wireframe shown in Figure 4.11.
Figure 4.11. A simple web site layout
133
Master Pages
If all the pages in the site have the same header, footer, and navigation menu, it
makes sense to include these components in a master page, and to build several
web forms that customize only the content areas on each page. Well begin to
create such a site in Chapter 5, but lets work through a quick example here.
To keep this example simple, we wont include a menu here: we'll include just
the header, the footer, and the content placeholder. In your Learning folder,
create a new file named FrontPages.master, and write the following code into
it:
File: FrontPages.master (excerpt)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Front Page</title>
</head>
<body>
<form id="myForm" runat="server">
<h1>Welcome to SuperSite Inc!</h1>
<asp:ContentPlaceHolder id="FrontPageContent"
runat="server" />
<p>Copyright 2006</p>
</form>
</body>
</html>
The master page looks almost like a web form, except for one important detail:
it has an empty ContentPlaceHolder control. If you want to build a web form
based on this master page, you just need to reference the master page using the
Page directive in the web form, and add a Content control that includes the
content you want to insert.
Let's try it. Create a web form called FrontPage.aspx, and add this code to it:
File: FrontPage.aspx (excerpt)
<%@ Page MasterPageFile="FrontPages.master" %>
<asp:Content id="myContent" runat="server"
ContentPlaceHolderID="FrontPageContent">
<p>
Welcome to our web site! We hope you'll enjoy your visit.
</p>
</asp:Content>
134
Chapter 4: Constructing ASP.NET Web Pages

Get Build Your Own ASP.NET 2.0 Web Site Using C# & VB, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.