使用子内容页面隐藏主页面内的主页面ContentPlaceHolder

时间:2013-08-23 19:05:21

标签: c# asp.net

我试图在任何地方找到这个解决方案,但我无法使其发挥作用。

我有以下代码。

MasterPage.master:

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

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

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:

<%@ 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>

我在my.aspx代码中尝试了以下内容,以隐藏ChildMasterPage.master中的“SideNav”面板:

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)

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)

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

在尝试加载使用ChildMasterPage的aspx页面时,所有这些都会给我以下错误:

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

最终,我想让它显示没有样式,所以在aspx页面上没有使用空格。像这样的东西,它给了我同样的错误:

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

提前感谢您提供的任何帮助。此外,如果这个问题已在这里得到解答,我提前道歉,但我无法找到它。至少没有任何效果。我对C#仍然不太满意,但我到了那里。

由于

标记

2 个答案:

答案 0 :(得分:2)

我认为您在第一种方法中已将Content控件类型转换为ContentPlaceHolder控件。

我相信代码应如下所示:

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

希望这会有所帮助!!

答案 1 :(得分:0)

我没有尝试强制my.aspx.cs“看到”ChildMasterPage.master上的Panel,而是执行了以下操作:

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

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

这会将Child Master页面的类转换为一个新变量,然后该变量可以作用于,或者从后面的my.aspx代码中调用Child Master Page代码中的函数。感谢我的同事搞清楚这一点。