I've tried to find this solution everywhere, but am unable to make it work.
I have the following code.
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>
I've tried the following in the my.aspx code behind to hide the "SideNav" Panel from the ChildMasterPage.master:
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;
}
All of these give me the following error when trying to load the aspx page that uses the ChildMasterPage:
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. 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.
Thanks
Mark
I think you have type cast the Content
control into ContentPlaceHolder
control in your first approach.
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
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;
}
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. Thanks goes to my co-worker for figuring this one out.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.