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就应该写多少个。

     

     

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

     

     
  • 相关阅读:
    failonerror on MSBuild
    近期Windows Mobile问题汇总
    android的文件操作 sdcard和rom
    用实际库存数调整批次保留最新的批次
    各种布局layout
    javascript让ui线程让出时间片的模型
    android ListView控件操作绑定数据、单击事件
    Pocket PC 模拟器上网设置
    android单元测试
    打电话发短信
  • 原文地址:https://www.cnblogs.com/leftfist/p/4258287.html
Copyright © 2011-2022 走看看