Solved

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

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

791 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