Link to home
Start Free TrialLog in
Avatar of Bobby X
Bobby XFlag for United States of America

asked on

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.
Avatar of wht1986
wht1986
Flag of United States of America image

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

Avatar of Bobby X

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of wht1986
wht1986
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bobby X

ASKER

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.

Avatar of Bobby X

ASKER

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

Thanks very much for all the help.