Programmatically hide a ContentPlaceHolder of a Master Page

Hi,

Is there a way to programmatically hide a ContentPlaceHolder of a Master Page during Page_Load of a content page in asp.net using C#? I need to hide a ContentPlaceHolder in a master page from a content page during runtime or during Page_Load of a content page.

Thanks in advance.
WebAppDeveloperAsked:
Who is Participating?
 
wht1986Connect With a Mentor Commented:
Put the public method on the code behind,

I believe
    public partial class MyMasterPage : System.Web.UI.MasterPage
should be
    public partial class MyMaster : System.Web.UI.MasterPage
instead

MyMasterPage was the name of my class, from the above it looks like yours is called MyMaster

You also have code behind on the master page, i would move the SetContentVisibility there instead
0
 
wht1986Commented:
I always like to add public method and properties to my master page. This way I can access easily anything i want on derived pages.
On the master page
 
public partial class MyMasterPage : System.Web.UI.MasterPage
{
    public void SetContentVisibility(bool isVisible)
    {
        this.ContentPlaceHolder1.Visible = isVisible;
    }
}
 
 
===========
 
On the derived page
 
public partial class Default2 : System.Web.UI.Page
{
    protected void Button1_Click(object sender, EventArgs e)
    {
        MyMasterPage mp = (MyMasterPage)this.Master;
        mp.SetContentVisibility(false);
    }
}

Open in new window

0
 
WebAppDeveloperAuthor Commented:
Hi wht1986,

In my master page, I'm getting a red underline on "MyContentHolder" with Intellisense of VS 2008 indicating something's wrong and also a red underline on "SecondLevelMasterPage" in my code-behind page. Please see code below.

Thanks.
My Master Page:
 
<%@ Master Language="C#" AutoEventWireup="true" EnableViewState="true" CodeFile="MyMaster.master.cs" Inherits="MyMaster" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<script runat="server">
    public partial class MyMasterPage : System.Web.UI.MasterPage
    {
        public void SetContentVisibility(bool isVisible)
        {
            this.MyContentHolder.Visible = isVisible;
        }
    }
</script>
 
<asp:ContentPlaceHolder ID="MyContentHolder" runat="server">         </asp:ContentPlaceHolder>
 
My Content Page's directive:
<%@ Page Language="C#" MasterPageFile="MyMaster.master" AutoEventWireup="true" CodeFile="solutions.aspx.cs" Inherits="solutions" %>
 
My Content Page's code-behind:
public partial class solutions : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SecondLevelMasterPage mp = (SecondLevelMasterPage)this.Master;
        mp.SetContentVisibility(false);
     }
}

Open in new window

0
 
WebAppDeveloperAuthor Commented:
wht1986,

It works. I'll accept it as solution in a minute. I have another question. If I turn a regular html <DIV ID="myDiv"> tag into a server control by adding the "runat=server" attribute to the tag (DIV), can I use this same approach (same code) to hide this <DIV>? If yes and the code is different, would you please provide the code as well.

Thanks very  much in advance.

0
 
WebAppDeveloperAuthor Commented:
hi wht1986,
I tried the same code for hiding a <DIV>, and it worked.

Thanks very much for all the help.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.