VS2008(C#)子页嵌套母版页的控件访问方法(三)

VS2008(C#)子页嵌套母版页的控件访问方法(三)——嵌套三层母版页的子页

 

子页嵌套了三层母版页后,依次访问第一层、第二层、第三层母版页控件的实现方法如下:

 

第一层母版页HTML代码

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage_MasterPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>MasterPage</title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        第一层母版页
        <br />
        <asp:Label ID="Master_Label" runat="server" Text="母版页(Label)"></asp:Label>
        <br />
        <br />
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

 

第二层母版页HTML代码

<%@ Master Language="C#" MasterPageFile="~/MasterPage/MasterPage.master" AutoEventWireup="true"
    CodeFile="MasterPage2.master.cs" Inherits="MasterPage_MasterPage2" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
    <%--添加第二层母版页的子页内容--%>
    <asp:ContentPlaceHolder ID="head2" runat="Server">
    </asp:ContentPlaceHolder>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    第二层母版页
    <br />
    <asp:Label ID="Master2_Label" runat="server" Text="母版页2(Label)"></asp:Label>
    <br />
    <br />
     <%--添加第二层母版页的子页内容--%>
    <asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="Server">
    </asp:ContentPlaceHolder>
</asp:Content>

 

第三层母版页HTML代码

<%@ Master Language="C#" MasterPageFile="~/MasterPage/MasterPage2.master" AutoEventWireup="true"
    CodeFile="MasterPage3.master.cs" Inherits="MasterPage_MasterPage3" %>

<%-- Add content controls here --%>
<%--定义第三层母版页的内容项--%>
<asp:Content ID="Content1" ContentPlaceHolderID="head2" runat="Server">
    <asp:ContentPlaceHolder ID="head3" runat="Server">
    </asp:ContentPlaceHolder>
</asp:Content>
<%--定义第三层母版页的内容项--%>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" runat="Server">
    第三层母版页
    <br />
    <asp:Label ID="Master3_Label" runat="server" Text="母版页3(Label)"></asp:Label>
    <br />
    <br />
    <asp:ContentPlaceHolder ID="ContentPlaceHolder3" runat="Server">
    </asp:ContentPlaceHolder>
</asp:Content>

 

子页前台HTML代码

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage/MasterPage3.master" AutoEventWireup="true" CodeFile="SubPage3.aspx.cs" Inherits="MasterPage_SubPage3" %>

<%-- Add content controls here --%>
<%--继承第三层母版页的正文内容--%>
<asp:Content ID="Content1" ContentPlaceHolderID="head3" runat="Server">
</asp:Content>
<%--继承第三层母版页的正文内容--%>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder3" runat="Server">
    子页--333
    <br />
    <asp:Label ID="Sub3_Label" runat="server" Text="Label"></asp:Label>
    <br />
    <br />
    <asp:Button ID="Button1" runat="server" Text="子页3访问母页3"
    onclick="Button1_Click" />
    <asp:Button ID="Button2" runat="server" Text="子页3注销母页3"
        onclick="Button2_Click" />
    <br />
    <br />
    <asp:Button ID="Button3" runat="server" Text="子页3访问母页2"
        onclick="Button3_Click" />
    <asp:Button ID="Button4" runat="server" Text="子页3注销母页2"
        onclick="Button4_Click" />
    <br />
    <br />
    <asp:Button ID="Button5" runat="server" Text="子页3访问母页1"
        onclick="Button5_Click" />
    <asp:Button ID="Button6" runat="server" Text="子页3注销母页1"
        onclick="Button6_Click" />
    <br />
</asp:Content>

 

子页后台CS代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MasterPage_SubPage3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    //访问第三层母板控件
    //登陆
    protected void Button1_Click(object sender, EventArgs e)
    {
        //从第三层得到第一层的ContentPlaceHolder,来访问其中的控件(此时已为第二层)
        ContentPlaceHolder cpMaster1 = (ContentPlaceHolder)Master.Master.Master.FindControl("ContentPlaceHolder1");
        //第一层母板找到第二层母板的ContentPlaceHolder,来访问其中的控件(此时已为第三层)
        ContentPlaceHolder cpMaster2 = (ContentPlaceHolder)cpMaster1.FindControl("ContentPlaceHolder2");
        Label m3Label = (Label)cpMaster2.FindControl("Master3_Label");
        m3Label.Text = "子页3 - 访问母版页3";
    }
    //注销
    protected void Button2_Click(object sender, EventArgs e)
    {
        //从第三层得到第一层的ContentPlaceHolder,来访问其中的控件(此时已为第二层)
        ContentPlaceHolder cpMaster1 = (ContentPlaceHolder)Master.Master.Master.FindControl("ContentPlaceHolder1");
        //第一层母板找到第二层母板的ContentPlaceHolder,来访问其中的控件(此时已为第三层)
        ContentPlaceHolder cpMaster2 = (ContentPlaceHolder)cpMaster1.FindControl("ContentPlaceHolder2");
        Label m3Label = (Label)cpMaster2.FindControl("Master3_Label");
        m3Label.Text = "母版页3(Label)";
    }

    //访问第二层母板控件
    //登陆
    protected void Button3_Click(object sender, EventArgs e)
    {
        //从第三层得到第一层的ContentPlaceHolder,来访问其中的控件(此时已为第二层)
        ContentPlaceHolder cpMaster1 = (ContentPlaceHolder)Master.Master.Master.FindControl("ContentPlaceHolder1");
        Label m2Label = (Label)cpMaster1.FindControl("Master2_Label");
        m2Label.Text = "子页3 - 访问母版页2";
    }
    //注销
    protected void Button4_Click(object sender, EventArgs e)
    {
        //从第三层得到第一层的ContentPlaceHolder,来访问其中的控件(此时已为第二层)
        ContentPlaceHolder cpMaster1 = (ContentPlaceHolder)Master.Master.Master.FindControl("ContentPlaceHolder1");
        Label m2Label = (Label)cpMaster1.FindControl("Master2_Label");
        m2Label.Text = "母版页2(Label)";
    }

    //访问第一层母板控件
    //登陆
    protected void Button5_Click(object sender, EventArgs e)
    {
        Label mLabel = (Label)Master.Master.Master.FindControl("Master_Label");
        mLabel.Text = "子页3 - 访问母版页1";
    }
    //注销
    protected void Button6_Click(object sender, EventArgs e)
    {
        Label mLabel = (Label)Master.Master.Master.FindControl("Master_Label");
        mLabel.Text = "母版页1(Label)";
    }
}

源代码下载: VS2008(C#)中的ASP.NET子页嵌套多层母版页的控件访问方法

posted @ 2010-01-29 21:08  SpringMVC+Hibernate  阅读(203)  评论(0编辑  收藏  举报
友情链接:厦门事业单位