Asp.net : 访问嵌套模版页中的控件

如果要访问的控件位于母版页的 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>

转载于:https://www.cnblogs.com/Jasmin/archive/2007/04/19/719884.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值