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

  1. 云栖社区>
  2. 博客>
  3. 正文

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

技术小胖子 2017-12-14 02:50:00 浏览932
展开阅读全文
[索引页]
[源码下载]



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


作者:webabcd


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


关键
在内容页的头部加上母版页的强类型引用
<%--创建对母版页的强类型引用,并指定到母版页的虚拟路径--%> 
<%@ MasterType VirtualPath="~/MasterPage/MasterPage.master" %>
 
 
1、内容页传递数据到母版页 - 母版页创建一个公共方法,然后内容页通过“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>
 
 
次母板页
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>
 
MasterPage/MasterPage.master.cs
InBlock.gifusing System; 
InBlock.gifusing System.Data; 
InBlock.gifusing System.Configuration; 
InBlock.gifusing System.Collections; 
InBlock.gifusing System.Web; 
InBlock.gifusing System.Web.Security; 
InBlock.gifusing System.Web.UI; 
InBlock.gifusing System.Web.UI.WebControls; 
InBlock.gifusing System.Web.UI.WebControls.WebParts; 
InBlock.gifusing System.Web.UI.HtmlControls; 
InBlock.gif 
InBlock.gifpublic partial class MasterPage_MasterPage : System.Web.UI.MasterPage 
InBlock.gif
InBlock.gif        protected void Page_Load(object sender, EventArgs e) 
InBlock.gif        { 
InBlock.gif 
InBlock.gif        } 
InBlock.gif 
InBlock.gif        /// <summary> 
InBlock.gif        /// 设置ddlMaster的选中索引 
InBlock.gif        /// 这个方法由内容页调用 
InBlock.gif        /// </summary> 
InBlock.gif        /// <param name="index"></param> 
InBlock.gif        public void SetddlMaster(int index) 
InBlock.gif        { 
InBlock.gif                ddlMaster.SelectedIndex = index; 
InBlock.gif        } 
InBlock.gif 
InBlock.gif        protected void ddlMaster_SelectedIndexChanged(object sender, EventArgs e) 
InBlock.gif        { 
InBlock.gif                // ddlMaster的选中索引改变后,激发SelectedIndexChanged_ddlMaster事件 
InBlock.gif                SelectedIndexChanged_ddlMaster(thisnew CommandEventArgs(ddlMaster.SelectedItem.Text, ddlMaster.SelectedValue)); 
InBlock.gif        } 
InBlock.gif 
InBlock.gif        // 声明一个公共时间事件,让内容页用 
InBlock.gif        public event CommandEventHandler SelectedIndexChanged_ddlMaster; 
InBlock.gif}
 
 
内容页
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>
 
MasterPage/Test.aspx.cs
InBlock.gifusing System; 
InBlock.gifusing System.Data; 
InBlock.gifusing System.Configuration; 
InBlock.gifusing System.Collections; 
InBlock.gifusing System.Web; 
InBlock.gifusing System.Web.Security; 
InBlock.gifusing System.Web.UI; 
InBlock.gifusing System.Web.UI.WebControls; 
InBlock.gifusing System.Web.UI.WebControls.WebParts; 
InBlock.gifusing System.Web.UI.HtmlControls; 
InBlock.gif 
InBlock.gifpublic partial class MasterPage_Test : System.Web.UI.Page 
InBlock.gif
InBlock.gif        protected void Page_Load(object sender, EventArgs e) 
InBlock.gif        { 
InBlock.gif                // 在内容页用FindControl方法找到内容页中的控件 
InBlock.gif                DropDownList ddl = new DropDownList(); 
InBlock.gif                ddl = Master.Master.FindControl("ContentPlaceHolder1").FindControl("cph").FindControl("ddlPage"as DropDownList; 
InBlock.gif                Master.Master.FindControl("ContentPlaceHolder1").FindControl("cph").Controls.Add(new LiteralControl("<br />内容页中的DropDownList的ClientID是:" + ddl.ClientID)); 
InBlock.gif 
InBlock.gif                // 增加一个事件处理,该事件是在母版页定义的一个公共事件 
InBlock.gif                Master.SelectedIndexChanged_ddlMaster += new CommandEventHandler(Master_SelectedIndexChanged_ddlMaster); 
InBlock.gif        } 
InBlock.gif 
InBlock.gif        void Master_SelectedIndexChanged_ddlMaster(object sender, CommandEventArgs e) 
InBlock.gif        { 
InBlock.gif                // CommandEventArgs已经在母版页中的公共事件“SelectedIndexChanged_ddlMaster”中指定 
InBlock.gif                string selectedText = e.CommandName; 
InBlock.gif                string selectedValue = e.CommandArgument.ToString(); 
InBlock.gif 
InBlock.gif                ddlPage.SelectedValue = selectedValue; 
InBlock.gif        } 
InBlock.gif 
InBlock.gif        protected void ddlPage_SelectedIndexChanged(object sender, EventArgs e) 
InBlock.gif        { 
InBlock.gif                // 调用母版页的方法 
InBlock.gif                Master.SetddlMaster(ddlPage.SelectedIndex); 
InBlock.gif        } 
InBlock.gif}
 
 
OK
[源码下载]




     本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/344833,如需转载请自行联系原作者



网友评论

登录后评论
0/500
评论
技术小胖子
+ 关注