zoukankan      html  css  js  c++  java
  • 嵌套母版页中的控件访问

    嵌套母版页中的控件访问

    左直拳

    嵌套母版页中的控件访问很别扭。

    如果一个内容页对应一个没有嵌套的母版页,访问这个母版页上的控件众所周知:类似(Button)Page.Master.FindControl("Button1")

    可是这个母版页如果又嵌套在另一个母版页里面,上述语句包你什么东西都访问不到。

    假设顶层母版页master0.master

            <asp:contentplaceholder id="SubMaster" runat="server">

            </asp:contentplaceholder>

    子母版页master1.master

    <asp:Content id="SubMasterList" ContentPlaceholderID="SubMaster" runat="server">   

    <asp:contentplaceholder id="Main" runat="server">

        </asp:contentplaceholder>

    <asp:Button ID="Button1" runat="server" Text="Button"/>

    </asp:Content>

    现在内容页content.aspx结合子母版页master1.master,有

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

    </asp:Content>

    这时不论是

    (Button)Page.Master.FindControl("Button1")

    还是

    ContentPlaceHolder direcMaster = (ContentPlaceHolder)Page.Master.FindControl("SubMaster");

    Button b1 = (Button)direcMaster.FindControl("Button1");

    都无法访问到这个BUTTON

    我折腾来折腾去,最后才知道正确的写法是:

    ContentPlaceHolder direcMaster = (ContentPlaceHolder)Page.Master.Master.FindControl("SubMaster");

    Button b1 = (Button)direcMaster.FindControl("Button1");

    就是说,如果母版页嵌套多少层,Master就应该写多少个。

     

     

    这样子的话,我认为如果想访问母版页的控件,还不如通过在母版页设置属性来间接访问该控件。一方面,访问方便;另一方面,可屏蔽细节,内容页根本不用关心所用的母版页到底嵌套了多少层。

     

     
  • 相关阅读:
    Unity 5.3 Assetbundle热更资源
    自定义协同程序:CustomYieldInstruction
    C# 温故而知新: 线程篇(四)
    C# 温故而知新: 线程篇(三)
    C# 温故而知新: 线程篇(二)
    c# 温故而知新: 线程篇(一)
    C# 温故而知新:Stream篇(六)
    C# 温故而知新:Stream篇(七)
    C# 温故而知新:Stream篇(四)
    Redis高级数据类型
  • 原文地址:https://www.cnblogs.com/leftfist/p/4258287.html
Copyright © 2011-2022 走看看