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

[索引页]
[源码下载]



温故知新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.gif using System; 
InBlock.gif using System.Data; 
InBlock.gif using System.Configuration; 
InBlock.gif using System.Collections; 
InBlock.gif using System.Web; 
InBlock.gif using System.Web.Security; 
InBlock.gif using System.Web.UI; 
InBlock.gif using System.Web.UI.WebControls; 
InBlock.gif using System.Web.UI.WebControls.WebParts; 
InBlock.gif using System.Web.UI.HtmlControls; 
InBlock.gif 
InBlock.gif public 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.gif using System; 
InBlock.gif using System.Data; 
InBlock.gif using System.Configuration; 
InBlock.gif using System.Collections; 
InBlock.gif using System.Web; 
InBlock.gif using System.Web.Security; 
InBlock.gif using System.Web.UI; 
InBlock.gif using System.Web.UI.WebControls; 
InBlock.gif using System.Web.UI.WebControls.WebParts; 
InBlock.gif using System.Web.UI.HtmlControls; 
InBlock.gif 
InBlock.gif public 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}
 
 




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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值