虽然母版页和内容页功能强大,但是其创建和应用过程并不复杂。本节和下一节将以创建如图1所示示例为例,向读者详细介绍,使用Visual Stuido 2005创建母版页和内容页的方法以及相关知识。本节的重点是创建母版页的方法。
母版页中包含的是页面公共部分,即网页模板。因此,在创建示例之前,必须判断哪些内容是页面公共部分,这就需要从分析页面结构开始。图1所示显示的是一个页面截图。在下文中,暂称该页面名为Index.aspx,并且假设其为某网站中的一页。通过分析可知,该页面的结构如图5所示。

图5 页面结构图
页面Index.aspx由4个部分组成:页头、页尾、内容1和内容2。其中页头和页尾是Index.aspx所在网站中页面的公共部分,网站中许多页面都包含相同的页头和页尾。内容1和内容2是页面的非公共部分,是Index.aspx页面所独有的。结合母版页和内容页的有关知识可知,如果使用母版页和内容页来创建页面Index.aspx,那么必须创建一个母版页MasterPage.master和一个内容页Index.aspx。其中母版页包含页头和页尾等内容,内容页中则包含内容1和内容2。
使用Visual Studio 2005创建一个普通Web站点,然后,在站点根目录下创建一个名为MasterPage.master的母版页。由于这是一个添加新文件的过程,因此,单击“网站”命令菜单中的“添加新项..”选项,可以打开如图6所示的窗口。

图6 添加母版页
由于此例创建的是母版页,因此,需要选择母版页图标,并且设置文件名为MasterPage.master。需要注意的是,该窗口中还有一个复选框项 “将代码放在单独的文件中”。默认情况下,该复选框处于选中状态。表示Visual Studio 2005将会为MasterPage.master文件应用代码隐藏模型,即在创建MasterPage.master文件的基础上,自动创建一个与该文件相关的MasterPage.master.cs文件。如果不选中该项,那么只会创建一个MasterPage.master文件而已。建议读者选取该项。
在创建MasterPage.master文件之后,接着就可以开始编辑该文件了。根据前文说明,母版页中只包含页面公共部分,因此,MasterPage.master中主要包含的是页头和页尾的代码。具体源代码如下所示(母版页MasterPage.master文件源代码):
1
2
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
3
4
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
<html xmlns="http://www.w3.org/1999/xhtml">
6
<head id="Head1" runat="server">
7
<title></title>
8
<link href="css/myfreetemplates.css" rel="stylesheet" type="text/css" />
9
</head>
10
11
<body background="images/pixi_lime.gif" leftmargin="0" topmargin="0">
12
<form id="form1" runat="server">
13
<div align="center">
14
<table width="763" height="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
15
<tr>
16
<td width="763" height="86" align="right" valign="top">
17
<img src="images/topic.gif"></td>
18
</tr>
19
<tr>
20
<td width="763" height="53" align="right" valign="bottom" background="images/nav_bg.gif"></td>
21
</tr>
22
<tr>
23
<td width="763" height="22" align="right" valign="top"><img src="images/toppic2.gif" width="763" height="22"></td>
24
</tr>
25
<tr>
26
<td width="763" valign="top">
27
<table width="100%" border="0" cellspacing="0" cellpadding="0">
28
<tr>
29
<td width="244" valign="top">
30
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"></asp:ContentPlaceHolder>
31
</td>
32
<td valign="top" align="left">
33
<asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server"></asp:ContentPlaceHolder>
34
</td>
35
</tr>
36
</table>
37
</td>
38
</tr>
39
<tr>
40
<td width="763" height="1" background="images/pixi_lime.gif"><img src="images/pixi_lime.gif" width="1" height="1"></td>
41
</tr>
42
<tr>
43
<td width="763" height="35" align="center" class="baseline">©Copyright Study.Com 2006</td>
44
</tr>
45
</table>
46
</div>
47
</form>
48
</body></html>

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

以上是母版页MasterPage.master的源代码,与普通的.aspx源代码非常相似,例如,包括<html>、<body>、<form>等 Web元素,但是,与普通页面还是存在差异。差异主要有两处(粗体代码所示)。差异一是代码头不同,母版页使用的是Master,而普通.aspx文件使用的是Page。除此之外,二者在代码头方面是相同的。差异二是母版页中声明了控件ContentPlaceHolder,而在普通.aspx文件中是不允许使用该控件的。在MasterPage.master的源代码中,共声明了两个ContentPlaceHolder控件,用于在页面模板中为内容1 和内容2占位。ContentPlaceHolder控件本身并不包含具体内容设置,仅是一个控件声明。
图7所示,显示了MasterPage.master文件的设计时视图。
图7 母版页设计时视图
使用Visual Studio 2005可以对母版页进行编辑,并且它完全支持“所见即所得”功能。无论是在代码模式下,还是设计模式下,使用Visual Studio 2005编辑母版页的方法,与编辑普通.aspx文件是相同的。图中两个矩形框表示ContentPlaceHolder控件。开发人员可以直接在矩形框中添加内容,所设置内容的代码将包含在ContentPlaceHolder控件声明代码中。需要注意的是,这种方法是,不规范的,因此,不推荐使用这种做法。