Solved

How to find out if a page has a certain contentplaceholder from within master page codebehind?

Posted on 2009-07-15
5
488 Views
Last Modified: 2012-05-07
Hi, I'm trying to find out, from my master page code behind, if a page includes a certain content placeholder.

If the page includes the content placeholder I need to add a class in my master page and show a panel.

I've tried looping through Page.Controls but it doesn't seem to be giving me what i want.
foreach (System.Web.UI.Control ctrl in Page.Controls)
		{
			if (ctrl.ID == "focusAreaPlaceHolder")
			{
				controlsinfo.Text = "found it";
				return;
			}
		}

Open in new window

0
Comment
Question by:bruno
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 4

Expert Comment

by:ki_ki
ID: 24861879
is there a Page.Findcontrol("focusAreaPlaceHolder")   ??
0
 
LVL 18

Author Comment

by:bruno
ID: 24861942
That throws an error.
Object reference not set to an instance of an object.
0
 
LVL 15

Expert Comment

by:jinal
ID: 24862324
Try

Master.Findcontrol("focusAreaPlaceHolder")

or

Page.Master.Findcontrol("focusAreaPlaceHolder")
0
 
LVL 15

Expert Comment

by:jinal
ID: 24862364
b'coz Page does not contain content place holder. When you apply master page to content page it adds

<asp:Content ID="content1" ContentPlaceHolderID="focusAreaPlaceHolder" runat="server">
</asp:Content>

so you use like this may be

foreach (System.Web.UI.Control ctrl in Page.Controls)
                {
                        Content c = ctrl as Content;
                        if( c != null)
                        {
                        if (c.ContentPlaceHolderID == "focusAreaPlaceHolder")
                        {
                                controlsinfo.Text = "found it";
                                return;
                        }
                    }
                }
0
 
LVL 18

Accepted Solution

by:
bruno earned 0 total points
ID: 24862570
@jinal,

that last block of code doesn't print out anything in my label. not sure if it's getting into the if statement or not, didn't debug.



@all,

I started thinking of it a bit differently...i realize the contentplaceholder was always in the controls collection on the master page, so I wondered if maybe I could just figure out if it was empty or not. I found this article which helped:
http://programcsharp.com/blog/archive/2009/01/22/test-if-masterpage-contentplaceholder-has-content-or-is-empty.aspx

That returns true or false depending on whether the page itself has the <asp:content> tag on it for the particular placeholder.

I realize now my original question wording was a bit misleading. I wasn't looking for the placeholder itself, I was looking for the <asp:content> tag that was associated with a certain placeholder.
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

734 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