母版页(MasterPage)就相当于模板页,挺简单的,没什么好说的。基于母版页的常用的功能有:母版页和内容页之间信息的传递,在内容页中用FindControl方法找到内容页中的控件等。另外,母版页是可以嵌套的。
[索引页]
[源码下载]



温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)


作者:webabcd


介绍
母版页(MasterPage)就相当于模板页,挺简单的,没什么好说的。基于母版页的常用的功能有:母版页和内容页之间信息的传递,在内容页中用FindControl方法找到内容页中的控件等。另外,母版页是可以嵌套的。


关键
在内容页的头部加上母版页的强类型引用
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_母版页温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_c#_02<%温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_03--创建对母版页的强类型引用,并指定到母版页的虚拟路径--%>
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_母版页温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_c#_02
<%温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_03@ MasterType VirtualPath="~/MasterPage/MasterPage.master" %>

1、内容页传递数据到母版页 - 母版页创建一个公共方法,然后内容页通过“Master.方法”来调用这个公共方法

2、母版页传递数据到内容页 - 母版页创建一个公共事件来传递数据,然后内容页处理这个事件

3、内容页中用FindControl方法找到内容页中的控件 - 用“Master.FindControl("ContentPlaceHolder1").FindControl("你要查找的控件ID")”来查找

4、嵌套母版页 - 说起来麻烦,看源码吧


示例
主母板页
Site.master
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_母版页温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_c#_02<%温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_03@ Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="Site" %>
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10
<html xmlns="http://www.w3.org/1999/xhtml">
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10
<head id="Head1" runat="server">
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10    
<title>重新过一遍ASP.NET 2.0(C#)</title>
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10
</head>
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10
<body>
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10    
<form id="form1" runat="server">
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10        
<div>
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10            
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10            
</asp:ContentPlaceHolder>
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10        
</div>
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10    
</form>
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10
</body>
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10
</html>
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10

次母板页
MasterPage/MasterPage.master
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_母版页温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_c#_02<%温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_03@ Master Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_控件_27    CodeFile
="MasterPage.master.cs" Inherits="MasterPage_MasterPage" 
%>
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10    
<p>
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10        我是一个嵌套母版页
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10    
</p>
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10    
<p>
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10        母版页中的内容
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10        
<asp:DropDownList ID="ddlMaster" runat="server" DataSourceID="XmlDataSource1" DataTextField="text"
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10            DataValueField
="value" AutoPostBack="True" OnSelectedIndexChanged="ddlMaster_SelectedIndexChanged">
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10        
</asp:DropDownList><asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/Config/DropDownListData.xml">
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10        
</asp:XmlDataSource>
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10    
</p>
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10    
<p>
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10        内容页中的内容
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10        
<asp:ContentPlaceHolder ID="cph" runat="Server" />
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10    
</p>
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10
</asp:Content>
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10

MasterPage/MasterPage.master.cs
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10using System;
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10
using System.Data;
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10
using System.Configuration;
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10
using System.Collections;
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10
using System.Web;
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10
using System.Web.Security;
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10
using System.Web.UI;
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10
using System.Web.UI.WebControls;
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10
using System.Web.UI.WebControls.WebParts;
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10
using System.Web.UI.HtmlControls;
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10
public partial class MasterPage_MasterPage : System.Web.UI.MasterPage
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_母版页温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_c#_02
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_03{
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_内容页_60    
protected void Page_Load(object sender, EventArgs e)
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_控件_61温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_c#_62    
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_03{
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_内容页_60
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_控件_65    }

温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_内容页_60
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_控件_61温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_c#_62    
/**//// <summary>
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_内容页_60    
/// 设置ddlMaster的选中索引
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_内容页_60    
/// 这个方法由内容页调用
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_内容页_60    
/// </summary>
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_控件_65    
/// <param name="index"></param>

温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_内容页_60    public void SetddlMaster(int index)
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_控件_61温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_c#_62    
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_03{
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_内容页_60        ddlMaster.SelectedIndex 
= index;
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_控件_65    }

温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_内容页_60
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_内容页_60    
protected void ddlMaster_SelectedIndexChanged(object sender, EventArgs e)
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_控件_61温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_c#_62    
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_03{
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_内容页_60        
// ddlMaster的选中索引改变后,激发SelectedIndexChanged_ddlMaster事件
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_内容页_60
        SelectedIndexChanged_ddlMaster(thisnew CommandEventArgs(ddlMaster.SelectedItem.Text, ddlMaster.SelectedValue));
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_控件_65    }

温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_内容页_60
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_内容页_60    
// 声明一个公共时间事件,让内容页用
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_内容页_60
    public event CommandEventHandler SelectedIndexChanged_ddlMaster;
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_控件_27}

温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10

内容页
MasterPage/Test.aspx
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_母版页温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_c#_02<%温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_03@ Page Language="C#" MasterPageFile="~/MasterPage/MasterPage.master" AutoEventWireup="true"
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_控件_27    CodeFile
="Test.aspx.cs" Inherits="MasterPage_Test" Title="MasterPage测试" 
%>
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_母版页温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_c#_02
<%温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_03--创建对母版页的强类型引用,并指定到母版页的虚拟路径--%>
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_母版页温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_c#_02
<%温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_03@ MasterType VirtualPath="~/MasterPage/MasterPage.master" %>
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10
<asp:Content ID="Content1" ContentPlaceHolderID="cph" runat="Server">
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10    
<asp:dropdownlist id="ddlPage" runat="server" datasourceid="XmlDataSource1" datatextfield="text"
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10        datavaluefield
="value" autopostback="True" onselectedindexchanged="ddlPage_SelectedIndexChanged">
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10    
</asp:dropdownlist>
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10    
<asp:xmldatasource id="XmlDataSource1" runat="server" datafile="~/Config/DropDownListData.xml">
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10    
</asp:xmldatasource>
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10
</asp:Content>
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10

MasterPage/Test.aspx.cs
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10using System;
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10
using System.Data;
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10
using System.Configuration;
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10
using System.Collections;
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10
using System.Web;
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10
using System.Web.Security;
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10
using System.Web.UI;
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10
using System.Web.UI.WebControls;
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10
using System.Web.UI.WebControls.WebParts;
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10
using System.Web.UI.HtmlControls;
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10
public partial class MasterPage_Test : System.Web.UI.Page
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_母版页温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_c#_02
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_03{
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_内容页_60    
protected void Page_Load(object sender, EventArgs e)
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_控件_61温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_c#_62    
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_03{
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_内容页_60        
// 在内容页用FindControl方法找到内容页中的控件
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_内容页_60
        DropDownList ddl = new DropDownList();
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_内容页_60        ddl 
= Master.Master.FindControl("ContentPlaceHolder1").FindControl("cph").FindControl("ddlPage"as DropDownList;
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_内容页_60        Master.Master.FindControl(
"ContentPlaceHolder1").FindControl("cph").Controls.Add(new LiteralControl("<br />内容页中的DropDownList的ClientID是:" + ddl.ClientID));
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_内容页_60
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_内容页_60        
// 增加一个事件处理,该事件是在母版页定义的一个公共事件
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_内容页_60
        Master.SelectedIndexChanged_ddlMaster += new CommandEventHandler(Master_SelectedIndexChanged_ddlMaster);
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_控件_65    }

温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_内容页_60
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_内容页_60    
void Master_SelectedIndexChanged_ddlMaster(object sender, CommandEventArgs e)
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_控件_61温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_c#_62    
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_03{
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_内容页_60        
// CommandEventArgs已经在母版页中的公共事件“SelectedIndexChanged_ddlMaster”中指定
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_内容页_60
        string selectedText = e.CommandName;
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_内容页_60        
string selectedValue = e.CommandArgument.ToString();
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_内容页_60
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_内容页_60        ddlPage.SelectedValue 
= selectedValue;
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_控件_65    }

温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_内容页_60
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_内容页_60    
protected void ddlPage_SelectedIndexChanged(object sender, EventArgs e)
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_控件_61温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_c#_62    
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_03{
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_内容页_60        
// 调用母版页的方法
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_内容页_60
        Master.SetddlMaster(ddlPage.SelectedIndex);
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_控件_65    }

温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_控件_27}

温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)_xml_10


OK
[源码下载]