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

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

 

嵌套一层母版页后,子页访问母版页的方法

 

母版页HTML代码(后台无需CS代码)

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

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

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    子页--111
    <br />
    <asp:Label ID="Sub_Label" runat="server" Text="Label"></asp:Label>
    <br />
    <br />
    <asp:Button ID="Button1" runat="server" Text="子页1访问母页1"
    onclick="Button1_Click" />
    <asp:Button ID="Button2" runat="server" Text="子页1注销母页1"
        onclick="Button2_Click" />
    <br />
   
    <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_SubPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Sub_Label.Text = "子页Label";
    }
    // 访问母版页,并登陆
    protected void Button1_Click(object sender, EventArgs e)
    {
        Label mLabel = (Label)Master.FindControl("Master_Label");
        mLabel.Text = "子页 - 访问母版页";
    }
    // 访问母版页,并注销
    protected void Button2_Click(object sender, EventArgs e)
    {
        Label mLabel = (Label)Master.FindControl("Master_Label");
        mLabel.Text = "母版页(Label)";
    }
}

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

posted @ 2010-01-29 20:56  张长胜  阅读(139)  评论(0编辑  收藏  举报