简体   繁体   English

使用子内容页面在MasterPage ContentPlaceHolder中隐藏子MasterPage上的面板

[英]Hide Panel on Child MasterPage Inside MasterPage ContentPlaceHolder using Child Content Page

I've tried to find this solution everywhere, but am unable to make it work. 我试图在各处找到这种解决方案,但是无法使其正常工作。

I have the following code. 我有以下代码。

MasterPage.master: MasterPage.master:

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="My_MasterPage" %>

<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server"></asp:ContentPlaceHolder>

ChildMasterPage.master: ChildMasterPage.master:

<%@ Master Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="ChildMasterPage.master.cs" Inherits="My_ChildMasterPage" %>
<%@ Register TagPrefix="uc1" TagName="FileDirectoryOrganizer" Src="~/Controls/my.ascx" %>


<asp:Content ID="Content7" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

<asp:panel id="SideNav" runat="server">

    <aside id="sideBar">

        There are 2 User Controls <asp:uc1> inside of this area

    </aside>

</asp:panel>

<div>
    <asp:ContentPlaceHolder ID="contentBody" runat="server" />
    <hr />
    <h3>Related Topics</h3>
</div>

</asp:Content>

my.aspx: my.aspx:

<%@ Page Title="" Language="C#" MasterPageFile="~/SubDirectory/ChildMasterPage.master" AutoEventWireup="true" CodeFile="my.aspx.cs" Inherits="SubDirectory_my" %>
<%@ MasterType VirtualPath="~/SubDirectory/ChildMasterPage.master" %>
<%@ Reference VirtualPath="~/MasterPage.master" %>

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

I've tried the following in the my.aspx code behind to hide the "SideNav" Panel from the ChildMasterPage.master: 我在my.aspx代码中尝试了以下操作,以从ChildMasterPage.master隐藏“ SideNav”面板:

1) 1)

protected void Page_Load(object sender, EventArgs e)
{
    ContentPlaceHolder cpHolder = this.Master.FindControl("Content7") as ContentPlaceHolder;
    Panel p = cpHolder.FindControl("SideNav") as Panel;
    p.Visible = false;
}

2) 2)

protected void Page_Load(object sender, EventArgs e)
{
    ContentPlaceHolder cpHolder = this.Master.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;
    Panel p = cpHolder.FindControl("SideNav") as Panel;
    p.Visible = false;
}

3) 3)

protected void Page_Load(object sender, EventArgs e)
{
    Panel p = this.Master.FindControl("SideNav") as Panel;
    p.Visible = false;
}

All of these give me the following error when trying to load the aspx page that uses the ChildMasterPage: 当尝试加载使用ChildMasterPage的aspx页面时,所有这些给了我以下错误:

 500 - Internal server error.
 There is a problem with the resource you are looking for, and it cannot be displayed.

Ultimately, I'd like to get it to display none with a style so the space is not used on the aspx page. 最终,我希望它不显示任何样式,以便在aspx页面上不使用空格。 Something like this, which gives me the same error as above: 这样的事情,给了我与上面相同的错误:

protected void Page_Load(object sender, EventArgs e)
{
    ((Panel)this.Page.Master.FindControl("SideNav")).Style.Add("display", "none");
}

Thank you in advance for any assistance you may offer. 预先感谢您提供的任何帮助。 Also, I apologize in advance if this question has been answered here already, but I was unable to find it. 另外,如果您已经在这里回答了这个问题,我也表示歉意,但是我找不到它。 At least nothing that worked. 至少没有任何效果。 I'm still not very good with C#, but I'm getting there. 我对C#仍然不太满意,但是我到了。

Thanks 谢谢

Mark 标记

I think you have type cast the Content control into ContentPlaceHolder control in your first approach. 我认为您在第一种方法中已将Content控件类型转换为ContentPlaceHolder控件。

I believe the code should be like below: 我相信代码应如下所示:

Content cpHolder = this.Master.FindControl("Content7") as Content;

Hope this will help !! 希望这会有所帮助!

Instead of trying to force the my.aspx.cs to "see" the Panel on the ChildMasterPage.master, I did the following: 我没有尝试强迫my.aspx.cs在ChildMasterPage.master上“查看”面板,而是执行以下操作:

my.aspx.cs my.aspx.cs

public partial class SubDirectory_my : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        My_ChildMasterPage hideMySideBar = this.Master;
        hideMySideBar.HideSideBar();
    }

ChildMasterPage.master ChildMasterPage.master

public void HideSideBar()
{
    SideNav.Visible = false;
}

This casts the class of the Child Master page to a new variable, then the variable can act on, or call the function in the Child Master Page code behind from the my.aspx code behind. 这会将子级母版页的类强制转换为新变量,然后该变量可以作用于该子级,或从后面的my.aspx代码中调用子级母版页代码中的函数。 Thanks goes to my co-worker for figuring this one out. 感谢我的同事弄清楚这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM