母版页的功能为提高工作效率、降低开发和维护强度。母版页应用于网站标志、广告条、导航条、版权声明等内容。
常见母板页代码结构如下所示:
<%@ MasterLanguage="C#"AutoEventWireup="true"CodeBehind="Site1.master.cs"Inherits="WebApplication1.Site1"%>
<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<asp:ContentPlaceHolderID="head"runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolderID="ContentPlaceHolder1"runat="server">
</asp:ContentPlaceHolder>
</form>
</body>
</html>
母板页优点:
1)有利于站点修改和维护,降低开发人员的工作强度。
2)提供高效的内容整合能力。
3)有利于实现页面布局。
4)提供一种便于利用的对象模型。
创建母板页
创建一个母板页Site1.Master和一个内容页Main.Aspx。
母板页包含页头和页尾等内容,内容页中包含内容1和内容2
- 创建母板页
对母板页代码进行编辑:
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="WebApplication1.Site1" %>
<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style>
*
{
margin: 0;
padding: 0;
}
</style>
<asp:ContentPlaceHolderID="head"runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div style="width: 100%; height: 100px; background-color:Aqua">
handle
<asp:Label ID="lblTime"runat="server"Text="当前页面:"></asp:Label>
</div>
<div style="width: 1200px; margin: 0 auto;">
<asp:ContentPlaceHolderID="ContentPlaceHolder1"runat="server">
</asp:ContentPlaceHolder>
</div>
<div style="width: 100%; height: 100px; background-color:Fuchsia">
Footer
</div>
</form>
</body>
</html>
- 创建内容页
内容页Main.aspx的源代码:
<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true"
CodeBehind="Main.aspx.cs" Inherits="WebApplication1.Main" %>
<%@ MasterType VirtualPath="~/Site1.Master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div style="width: 100%; height: 100px; background-color: Purple">
Main内容页
</div>
</asp:Content>
如图显示Main.aspx设计视图
- 访问母板页
内容包括:使用FindControl,获取母板页控件的实现方法
使用MasterType指令,获取母板页控件引用的实现方法,使用MasterType指令,获取母板页简单自定义属性的实现方法。
1)使用FindControl,获取母板页控件引用
从内容页获取母板页控件引用的方法
母板页Site1.Master源代码如下:
<%@ MasterLanguage="C#"AutoEventWireup="true"CodeBehind="Site1.master.cs"Inherits="WebApplication1.Site1"%>
<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style>
*
{
margin: 0;
padding: 0;
}
</style>
<asp:ContentPlaceHolderID="head"runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div style="width: 100%; height: 100px; background-color:Aqua">
handle
<asp:Label ID="lblTime"runat="server"Text="当前页面:"></asp:Label>
</div>
<div style="width: 1200px; margin: 0 auto;">
<asp:ContentPlaceHolderID="ContentPlaceHolder1"runat="server">
</asp:ContentPlaceHolder>
</div>
<div style="width: 100%; height: 100px; background-color:Fuchsia">
Footer
</div>
</form>
</body>
</html>
内容页Main.aspx文件代码
using System;
usingSystem.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
usingSystem.Web.UI.WebControls;
namespace WebApplication1
{
public partial class Main :System.Web.UI.Page
{
protected voidPage_Load(object sender, EventArgs e)
{
Label lb = Master.FindControl("lblTime") asLabel;
lb.Text += "Main.aspx";
}
}
}
2)使用MasterType指令,获取母板页控件引用
母板页后台代码:
using System;
usingSystem.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class Site1 :System.Web.UI.MasterPage
{
protected voidPage_Load(object sender, EventArgs e)
{
}
public string Msg
{
get
{
return lblTime.Text;
}
set
{
lblTime.Text += value;
}
}
}
}
内容源代码;
using System;
usingSystem.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
usingSystem.Web.UI.WebControls;
namespace WebApplication1
{
public partial class Main :System.Web.UI.Page
{
protected voidPage_Load(object sender, EventArgs e)
{
Master.Msg = "Main.aspx";
}
}
}
两种方法运行后如图所示: