使用子内容页在母版页ContentPlaceHolder内隐藏子母版页上的面板

本文关键字:母版页 隐藏 ContentPlaceHolder | 更新日期: 2023-09-27 18:28:45

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

我有以下代码。

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#,但我正在达到目标。

感谢

标记

使用子内容页在母版页ContentPlaceHolder内隐藏子母版页上的面板

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

我认为代码应该如下:

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

希望这会有所帮助!!

我没有试图强制my.aspx"查看"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;
}

这会将子母版页的类强制转换为一个新变量,然后该变量可以执行操作,或者从my.aspx代码隐藏调用子母版页面代码隐藏中的函数。感谢我的同事解决了这个问题。