[索引页]
[源码下载]
作者: webabcd
介绍
母版页(MasterPage)就相当于模板页,挺简单的,没什么好说的。基于母版页的常用的功能有:母版页和内容页之间信息的传递,在内容页中用FindControl方法找到内容页中的控件等。另外,母版页是可以嵌套的。
关键
在内容页的头部加上母版页的强类型引用
[源码下载]
温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)
作者: webabcd
介绍
母版页(MasterPage)就相当于模板页,挺简单的,没什么好说的。基于母版页的常用的功能有:母版页和内容页之间信息的传递,在内容页中用FindControl方法找到内容页中的控件等。另外,母版页是可以嵌套的。
关键
在内容页的头部加上母版页的强类型引用
<%--创建对母版页的强类型引用,并指定到母版页的虚拟路径--%>
<%@ MasterType VirtualPath="~/MasterPage/MasterPage.master" %>
<%@ MasterType VirtualPath="~/MasterPage/MasterPage.master" %>
1、内容页传递数据到母版页 - 母版页创建一个公共方法,然后内容页通过“Master.方法”来调用这个公共方法
2、母版页传递数据到内容页 - 母版页创建一个公共事件来传递数据,然后内容页处理这个事件
3、内容页中用FindControl方法找到内容页中的控件 - 用“Master.FindControl("ContentPlaceHolder1").FindControl("你要查找的控件ID")”来查找
4、嵌套母版页 - 说起来麻烦,看源码吧
示例
主母板页
Site.master
2、母版页传递数据到内容页 - 母版页创建一个公共事件来传递数据,然后内容页处理这个事件
3、内容页中用FindControl方法找到内容页中的控件 - 用“Master.FindControl("ContentPlaceHolder1").FindControl("你要查找的控件ID")”来查找
4、嵌套母版页 - 说起来麻烦,看源码吧
示例
主母板页
Site.master
%@ Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="Site" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>重新过一遍ASP.NET 2.0(C#)</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>重新过一遍ASP.NET 2.0(C#)</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
次母板页
MasterPage/MasterPage.master
MasterPage/MasterPage.master
<%@ Master Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="MasterPage.master.cs" Inherits="MasterPage_MasterPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<p>
我是一个嵌套母版页
</p>
<p>
母版页中的内容
<asp:DropDownList ID="ddlMaster" runat="server" DataSourceID="XmlDataSource1" DataTextField="text"
DataValueField="value" AutoPostBack="True" OnSelectedIndexChanged="ddlMaster_SelectedIndexChanged">
</asp:DropDownList><asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/Config/DropDownListData.xml">
</asp:XmlDataSource>
</p>
<p>
内容页中的内容
<asp:ContentPlaceHolder ID="cph" runat="Server" />
</p>
</asp:Content>
CodeFile="MasterPage.master.cs" Inherits="MasterPage_MasterPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<p>
我是一个嵌套母版页
</p>
<p>
母版页中的内容
<asp:DropDownList ID="ddlMaster" runat="server" DataSourceID="XmlDataSource1" DataTextField="text"
DataValueField="value" AutoPostBack="True" OnSelectedIndexChanged="ddlMaster_SelectedIndexChanged">
</asp:DropDownList><asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/Config/DropDownListData.xml">
</asp:XmlDataSource>
</p>
<p>
内容页中的内容
<asp:ContentPlaceHolder ID="cph" runat="Server" />
</p>
</asp:Content>
MasterPage/MasterPage.master.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class MasterPage_MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load( object sender, EventArgs e)
{
}
/// <summary>
/// 设置ddlMaster的选中索引
/// 这个方法由内容页调用
/// </summary>
/// <param name="index"></param>
public void SetddlMaster( int index)
{
ddlMaster.SelectedIndex = index;
}
protected void ddlMaster_SelectedIndexChanged( object sender, EventArgs e)
{
// ddlMaster的选中索引改变后,激发SelectedIndexChanged_ddlMaster事件
SelectedIndexChanged_ddlMaster( this, new CommandEventArgs(ddlMaster.SelectedItem.Text, ddlMaster.SelectedValue));
}
// 声明一个公共时间事件,让内容页用
public event CommandEventHandler SelectedIndexChanged_ddlMaster;
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class MasterPage_MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load( object sender, EventArgs e)
{
}
/// <summary>
/// 设置ddlMaster的选中索引
/// 这个方法由内容页调用
/// </summary>
/// <param name="index"></param>
public void SetddlMaster( int index)
{
ddlMaster.SelectedIndex = index;
}
protected void ddlMaster_SelectedIndexChanged( object sender, EventArgs e)
{
// ddlMaster的选中索引改变后,激发SelectedIndexChanged_ddlMaster事件
SelectedIndexChanged_ddlMaster( this, new CommandEventArgs(ddlMaster.SelectedItem.Text, ddlMaster.SelectedValue));
}
// 声明一个公共时间事件,让内容页用
public event CommandEventHandler SelectedIndexChanged_ddlMaster;
}
内容页
MasterPage/Test.aspx
MasterPage/Test.aspx
<%@ Page Language="C#" MasterPageFile="~/MasterPage/MasterPage.master" AutoEventWireup="true"
CodeFile="Test.aspx.cs" Inherits="MasterPage_Test" Title="MasterPage测试" %>
<%--创建对母版页的强类型引用,并指定到母版页的虚拟路径--%>
<%@ MasterType VirtualPath="~/MasterPage/MasterPage.master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="cph" runat="Server">
<asp:dropdownlist id="ddlPage" runat="server" datasourceid="XmlDataSource1" datatextfield="text"
datavaluefield="value" autopostback="True" onselectedindexchanged="ddlPage_SelectedIndexChanged">
</asp:dropdownlist>
<asp:xmldatasource id="XmlDataSource1" runat="server" datafile="~/Config/DropDownListData.xml">
</asp:xmldatasource>
</asp:Content>
CodeFile="Test.aspx.cs" Inherits="MasterPage_Test" Title="MasterPage测试" %>
<%--创建对母版页的强类型引用,并指定到母版页的虚拟路径--%>
<%@ MasterType VirtualPath="~/MasterPage/MasterPage.master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="cph" runat="Server">
<asp:dropdownlist id="ddlPage" runat="server" datasourceid="XmlDataSource1" datatextfield="text"
datavaluefield="value" autopostback="True" onselectedindexchanged="ddlPage_SelectedIndexChanged">
</asp:dropdownlist>
<asp:xmldatasource id="XmlDataSource1" runat="server" datafile="~/Config/DropDownListData.xml">
</asp:xmldatasource>
</asp:Content>
MasterPage/Test.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class MasterPage_Test : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
// 在内容页用FindControl方法找到内容页中的控件
DropDownList ddl = new DropDownList();
ddl = Master.Master.FindControl( "ContentPlaceHolder1").FindControl( "cph").FindControl( "ddlPage") as DropDownList;
Master.Master.FindControl( "ContentPlaceHolder1").FindControl( "cph").Controls.Add( new LiteralControl( "<br />内容页中的DropDownList的ClientID是:" + ddl.ClientID));
// 增加一个事件处理,该事件是在母版页定义的一个公共事件
Master.SelectedIndexChanged_ddlMaster += new CommandEventHandler(Master_SelectedIndexChanged_ddlMaster);
}
void Master_SelectedIndexChanged_ddlMaster( object sender, CommandEventArgs e)
{
// CommandEventArgs已经在母版页中的公共事件“SelectedIndexChanged_ddlMaster”中指定
string selectedText = e.CommandName;
string selectedValue = e.CommandArgument.ToString();
ddlPage.SelectedValue = selectedValue;
}
protected void ddlPage_SelectedIndexChanged( object sender, EventArgs e)
{
// 调用母版页的方法
Master.SetddlMaster(ddlPage.SelectedIndex);
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class MasterPage_Test : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
// 在内容页用FindControl方法找到内容页中的控件
DropDownList ddl = new DropDownList();
ddl = Master.Master.FindControl( "ContentPlaceHolder1").FindControl( "cph").FindControl( "ddlPage") as DropDownList;
Master.Master.FindControl( "ContentPlaceHolder1").FindControl( "cph").Controls.Add( new LiteralControl( "<br />内容页中的DropDownList的ClientID是:" + ddl.ClientID));
// 增加一个事件处理,该事件是在母版页定义的一个公共事件
Master.SelectedIndexChanged_ddlMaster += new CommandEventHandler(Master_SelectedIndexChanged_ddlMaster);
}
void Master_SelectedIndexChanged_ddlMaster( object sender, CommandEventArgs e)
{
// CommandEventArgs已经在母版页中的公共事件“SelectedIndexChanged_ddlMaster”中指定
string selectedText = e.CommandName;
string selectedValue = e.CommandArgument.ToString();
ddlPage.SelectedValue = selectedValue;
}
protected void ddlPage_SelectedIndexChanged( object sender, EventArgs e)
{
// 调用母版页的方法
Master.SetddlMaster(ddlPage.SelectedIndex);
}
}
OK
[源码下载]
[源码下载]
本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/344833,如需转载请自行联系原作者