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

     

     

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

     

     
  • 相关阅读:
    【143】360云盘资源
    【142】阿蛮歌霸使用技巧
    [置顶] 程序员必知(三):一分钟知道URI编码(encodeURI)
    [置顶] Oracle job procedure 存储过程定时任务
    浅析动态表单
    DoctorNote医生处方笔记开发记录
    Step2:配置Oracle Dataguard
    IOS开发UIImage中stretchableImageWithLeftCapWidth方法的解释
    解析客户端脚本、服务器端脚本
    tomcat目录结构
  • 原文地址:https://www.cnblogs.com/leftfist/p/4258287.html
Copyright © 2011-2022 走看看