zoukankan      html  css  js  c++  java
  • 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>

  • 相关阅读:
    阿里云内网和公网NTP服务器和其他互联网基础服务时间同步服务器
    python3 tkinter
    未来的趋势发展 802.11v网络协议解析
    如何挑选好料酒?
    bootstrap
    结巴中文词频分析
    Covariance 协方差分析
    调整的R方_如何选择回归模型
    赤池信息量准则 ( Akaike information criterion)
    python蒙特卡洛脚本模拟—挑战者号爆炸概率
  • 原文地址:https://www.cnblogs.com/Jasmin/p/719884.html
Copyright © 2011-2022 走看看