我试图在任何地方找到这个解决方案,但无法让它发挥作用。
我有以下代码。
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>
我的.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#,但我已经做到了。
谢谢
标记