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代码

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage/MasterPage2.master"
    AutoEventWireup="true" CodeFile="SubPage2.aspx.cs" Inherits="MasterPage_SubPage2" %>

<%-- Add content controls here --%>
<%--继承第二层母版页的正文内容--%>
<asp:Content ID="Content1" ContentPlaceHolderID="head2" runat="Server">
</asp:Content>
<%--继承第二层母版页的正文内容--%>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" runat="Server">
    子页--222
    <br />
    <asp:Label ID="Sub2_Label" runat="server" Text="Label"></asp:Label>
    <br />
    <br />
    <asp:Button ID="Button1" runat="server" Text="子页2访问母页2" OnClick="Button1_Click" />
    <asp:Button ID="Button2" runat="server" Text="子页2注销母页2" OnClick="Button2_Click" />
    <br />
    <br />
    <asp:Button ID="Button3" runat="server" Text="子页2访问母页1"
        onclick="Button3_Click" />
    <asp:Button ID="Button4" runat="server" Text="子页2注销母页1"
        onclick="Button4_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_SubPage2 : 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.FindControl("ContentPlaceHolder1");
        Label m2Label = (Label)cpMaster1.FindControl("Master2_Label");
        m2Label.Text = "子页2 - 访问母版页2";
    }
    //注销
    protected void Button2_Click(object sender, EventArgs e)
    {
        //从第二层得到第一层的ContentPlaceHolder,来访问其中的控件(此时已为第二层)
        ContentPlaceHolder cpMaster1 = (ContentPlaceHolder)Master.Master.FindControl("ContentPlaceHolder1");
        Label m2Label = (Label)cpMaster1.FindControl("Master2_Label");
        m2Label.Text = "母版页2(Label)";
    }

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

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

posted @ 2010-01-29 21:02  张长胜  阅读(120)  评论(0编辑  收藏  举报