Solved

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

Posted on 2009-07-15
5
485 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 SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

778 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