Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2009-07-15
5
Medium Priority
?
494 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Loops Section Overview
Screencast - Getting to Know the Pipeline

885 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