[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Programmatically hide a ContentPlaceHolder of a Master Page

Posted on 2009-04-23
5
Medium Priority
?
3,798 Views
Last Modified: 2013-11-05
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.
0
Comment
Question by:WebAppDeveloper
  • 3
  • 2
5 Comments
 
LVL 12

Expert Comment

by:wht1986
ID: 24221516
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
 

Author Comment

by:WebAppDeveloper
ID: 24221671
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
 
LVL 12

Accepted Solution

by:
wht1986 earned 2000 total points
ID: 24221715
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
 

Author Comment

by:WebAppDeveloper
ID: 24221906
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
 

Author Comment

by:WebAppDeveloper
ID: 24221943
hi wht1986,
I tried the same code for hiding a <DIV>, and it worked.

Thanks very much for all the help.

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Loops Section Overview
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question