Solved

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

Posted on 2009-07-15
5
483 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
  • 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now