0
点赞
收藏
分享

微信扫一扫

嵌套母版页中的控件访问


嵌套母版页中的控件访问

左直拳

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

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

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

假设顶层母版页master0.master有

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

        </asp:contentplaceholder>

子母版页master1.master有

<asp:Contentid="SubMasterList" ContentPlaceholderID="SubMaster" runat="server">   

<asp:contentplaceholderid="Main" runat="server">

    </asp:contentplaceholder>

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

</asp:Content>

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

<asp:ContentID="Content1" ContentPlaceHolderID="Main" Runat="Server">

</asp:Content>

这时不论是

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

还是

ContentPlaceHolderContentPlaceHolder)Page.Master.FindControl("SubMaster");

ButtonButton)direcMaster.FindControl("Button1");

都无法访问到这个BUTTON。

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

ContentPlaceHolderContentPlaceHolder)Page.Master.Master.FindControl("SubMaster");

ButtonButton)direcMaster.FindControl("Button1");

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

 

 

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

 

举报

相关推荐

0 条评论