含羞草
好记性不如烂笔头
菜鸟先飞...

如果要访问的控件位于母版页的 ContentPlaceHolder 控件内部,必须首先获取对 ContentPlaceHolder 控件的引用,然后调用其 FindControl 方法获取对该控件的引用。

在内容页的Page_Load方法中设置嵌套模版页中label控件的Text属性:
首先获取主模版页的ContentPlaceHolder控件的引用,然后再获取要修改的控件的引用。
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ContentPlaceHolder pContent = (ContentPlaceHolder)Page.Master.Master.FindControl("Main");
            if (pContent != null)
            {
                Label pLblTitle = (Label)pContent.FindControl("lbl_title");
                if (pLblTitle != null)
                {
                    pLblTitle.Text = "My Title";
                }
            }
        }
    }

附:
主模版页:
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="Master_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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:contentplaceholder id="Main" runat="server">
        </asp:contentplaceholder>
    </div>
    </form>
</body>
</html>

嵌套模版页:
<%@ Master Language="C#" MasterPageFile="~/Master/Default.master" AutoEventWireup="true" CodeFile="Combines.master.cs" Inherits="Master_Combines" %>

<asp:content id="Content_combine" contentplaceholderid="Main" runat="server">
    <asp:Label ID="lbl_title" runat="server" Text=""></asp:Label>
    <asp:contentplaceholder id="Combine" runat="server">
    </asp:contentplaceholder>
</asp:content>

内容页:
<%@ Page Language="C#" MasterPageFile="~/Master/Combines.master" AutoEventWireup="true" CodeFile="StationCombine.aspx.cs" Inherits="_Default" Title="Untitled Page" %>
<%@ MasterType VirtualPath="~/Master/Combines.master" %>

<asp:Content ID="Content1" ContentPlaceHolderID="Combine" Runat="Server">
</asp:Content>

posted on 2007-04-19 17:11  含羞草  阅读(1568)  评论(1编辑  收藏  举报