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

[索引页]
[源码下载]

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

作者:webabcd

介绍
母版页(MasterPage)就一定于模板页,挺不难的,没什么好说的。基于母版页的常用的效果有:母版页和情节页之间音信的传递,在内容页中用FindControl方法找到内容页中的控件等。别的,母版页是能够嵌套的。

关键
在情节页的头顶加上母版页的强类型引用

C# 1C# 2<%C# 3–创立对母版页的强类型引用,并钦定到母版页的虚拟路径–%>
C# 4C# 5<%C# 6@ MasterType VirtualPath=”~/MasterPage/MasterPage.master” %>

壹 、内容页传递数据到母版页 –
母版页创制二个共用措施,然后内容页通过“Master.方法”来调用这么些集体艺术

② 、母版页传递数据到内容页 –
母版页创建叁个公共事件来传递数据,然后内容页处理那个事件

三 、内容页中用FindControl方法找到内容页中的控件 –
用“Master.FindControl(“ContentPlaceHolder1”).FindControl(“你要寻找的控件ID”)”来搜寻

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

示例
主母板页
Site.master

C# 7C# 8<%C# 9@ Master Language=”C#” AutoEventWireup=”true” CodeFile=”Site.master.cs” Inherits=”Site” %>
C# 10<html xmlns=”http://www.w3.org/1999/xhtml"&gt;
C# 11<head id=”Head1″ runat=”server”>
C# 12    <title>重新过三回ASP.NET 2.0(C#)</title>
C# 13</head>
C# 14<body>
C# 15    <form id=”form1″ runat=”server”>
C# 16        <div>
C# 17            <asp:ContentPlaceHolder ID=”ContentPlaceHolder1″ runat=”server”>
C# 18            </asp:ContentPlaceHolder>
C# 19        </div>
C# 20    </form>
C# 21</body>
C# 22</html>
C# 23

次母板页
MasterPage/MasterPage.master

C# 24C# 25<%C# 26@ Master Language=”C#” MasterPageFile=”~/Site.master” AutoEventWireup=”true”
C# 27    CodeFile=”MasterPage.master.cs” Inherits=”MasterPage_MasterPage” %>
C# 28<asp:Content ID=”Content1″ ContentPlaceHolderID=”ContentPlaceHolder1″ runat=”Server”>
C# 29    <p>
C# 30        我是一个嵌套母版页
C# 31    </p>
C# 32    <p>
C# 33        母版页中的内容
C# 34        <asp:DropDownList ID=”ddlMaster” runat=”server” DataSourceID=”XmlDataSource1″ DataTextField=”text”
C# 35            DataValueField=”value” AutoPostBack=”True” OnSelectedIndexChanged=”ddlMaster_SelectedIndexChanged”>
C# 36        </asp:DropDownList><asp:XmlDataSource ID=”XmlDataSource1″ runat=”server” DataFile=”~/Config/DropDownListData.xml”>
C# 37        </asp:XmlDataSource>
C# 38    </p>
C# 39    <p>
C# 40        内容页中的内容
C# 41        <asp:ContentPlaceHolder ID=”cph” runat=”Server” />
C# 42    </p>
C# 43</asp:Content>
C# 44

MasterPage/MasterPage.master.cs

C# 45using System;
C# 46using System.Data;
C# 47using System.Configuration;
C# 48using System.Collections;
C# 49using System.Web;
C# 50using System.Web.Security;
C# 51using System.Web.UI;
C# 52using System.Web.UI.WebControls;
C# 53using System.Web.UI.WebControls.WebParts;
C# 54using System.Web.UI.HtmlControls;
C# 55
C# 56public partial class MasterPage_MasterPage : System.Web.UI.MasterPage
C# 57C# 58C# 59{
C# 60    protected void Page_Load(object sender, EventArgs e)
C# 61C# 62    C# 63{
C# 64
C# 65    }
C# 66
C# 67C# 68    /**//// <summary>
C# 69    /// 设置ddlMaster的当选索引
C# 70    /// 这几个方法由内容页调用
C# 71    /// </summary>
C# 72    /// <param name=”index”></param>
C# 73    public void SetddlMaster(int index)
C# 74C# 75    C# 76{
C# 77        ddlMaster.SelectedIndex = index;
C# 78    }
C# 79
C# 80    protected void ddlMaster_SelectedIndexChanged(object sender, EventArgs e)
C# 81C# 82    C# 83{
C# 84        // ddlMaster的入选索引改变后,激发SelectedIndexChanged_ddlMaster事件
C# 85        SelectedIndexChanged_ddlMaster(this, new CommandEventArgs(ddlMaster.SelectedItem.Text, ddlMaster.SelectedValue));
C# 86    }
C# 87
C# 88    // 声美赞臣个公家时间事件,让内容页用
C# 89    public event CommandEventHandler SelectedIndexChanged_ddlMaster;
C# 90}
C# 91

内容页
MasterPage/Test.aspx

C# 92C# 93<%C# 94@ Page Language=”C#” MasterPageFile=”~/MasterPage/MasterPage.master” AutoEventWireup=”true”
C# 95    CodeFile=”Test.aspx.cs” Inherits=”MasterPage_Test” Title=”MasterPage测试” %>
C# 96
C# 97C# 98<%C# 99–创立对母版页的强类型引用,并点名到母版页的虚拟路径–%>
C# 100C# 101<%C# 102@ MasterType VirtualPath=”~/MasterPage/MasterPage.master” %>
C# 103<asp:Content ID=”Content1″ ContentPlaceHolderID=”cph” runat=”Server”>
C# 104    <asp:dropdownlist id=”ddlPage” runat=”server” datasourceid=”XmlDataSource1″ datatextfield=”text”
C# 105        datavaluefield=”value” autopostback=”True” onselectedindexchanged=”ddlPage_SelectedIndexChanged”>
C# 106    </asp:dropdownlist>
C# 107    <asp:xmldatasource id=”XmlDataSource1″ runat=”server” datafile=”~/Config/DropDownListData.xml”>
C# 108    </asp:xmldatasource>
C# 109</asp:Content>
C# 110

MasterPage/Test.aspx.cs

C# 111using System;
C# 112using System.Data;
C# 113using System.Configuration;
C# 114using System.Collections;
C# 115using System.Web;
C# 116using System.Web.Security;
C# 117using System.Web.UI;
C# 118using System.Web.UI.WebControls;
C# 119using System.Web.UI.WebControls.WebParts;
C# 120using System.Web.UI.HtmlControls;
C# 121
C# 122public partial class MasterPage_Test : System.Web.UI.Page
C# 123C# 124C# 125{
C# 126    protected void Page_Load(object sender, EventArgs e)
C# 127C# 128    C# 129{
C# 130        // 在情节页用FindControl方法找到内容页中的控件
C# 131        DropDownList ddl = new DropDownList();
C# 132        ddl = Master.Master.FindControl(“ContentPlaceHolder1”).FindControl(“cph”).FindControl(“ddlPage”) as DropDownList;
C# 133        Master.Master.FindControl(“ContentPlaceHolder1”).FindControl(“cph”).Controls.Add(new LiteralControl(“<br />内容页中的DropDownList的ClientID是:” + ddl.ClientID));
C# 134
C# 135        // 增加八个事件处理,该事件是在母版页定义的一个公共事件
C# 136        Master.SelectedIndexChanged_ddlMaster += new CommandEventHandler(Master_SelectedIndexChanged_ddlMaster);
C# 137    }
C# 138
C# 139    void Master_SelectedIndexChanged_ddlMaster(object sender, CommandEventArgs e)
C# 140C# 141    C# 142{
C# 143        // Command伊芙ntArgs已经在母版页中的公共事件“SelectedIndexChanged_ddlMaster”中指定
C# 144        string selectedText = e.CommandName;
C# 145        string selectedValue = e.CommandArgument.ToString();
C# 146
C# 147        ddlPage.SelectedValue = selectedValue;
C# 148    }
C# 149
C# 150    protected void ddlPage_SelectedIndexChanged(object sender, EventArgs e)
C# 151C# 152    C# 153{
C# 154        // 调用母版页的方法
C# 155        Master.SetddlMaster(ddlPage.SelectedIndex);
C# 156    }
C# 157}
C# 158

OK
[源码下载]

相关文章