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>

  • 相关阅读:
    第七章 防火墙和网络地址转换
    第六章 系统配置:DHCP和自动配置
    VLOOKUP函数的用法
    orcad 里误给元件添加了属性,如何删除
    文件夹无法删除提示找不到该项目怎么办
    网址
    OrCAD16.6中对比两份DSN文件的方法
    cadence allegro pcb模块设计复用
    Allegro Desgin Compare的用法与网表比较
    转:office 2016最新安装及激活教程(KMS)
  • 原文地址:https://www.cnblogs.com/Jasmin/p/719884.html
Copyright © 2011-2022 走看看