Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
DZone MVB

Links

Social

Using parent page properties in user control

There may be situations when we need to use parent page properties from user control. I know, this situations is a warning sign - there's something wrong with UI structure if user controls should know their parents. So, how to get correct reference to parent page so we can use also custom properties defined there?

 

NB! This blog is moved to gunnarpeipman.com

Comments

Nick said:

I have a custom control base class that overrides page with the casted page, which makes this super easy to use.

# May 21, 2008 5:42 PM

DigiMortal said:

Well... IMHO it is not a good idea to have such a smart user controls that know about their parent pages. This is bad dependency because you cannot reuse these user controls on the other pages or other projects. I think this topic is also worth a blog entry.

# May 22, 2008 2:01 AM

xdpnuke said:

good,Control.Parent ,

# May 26, 2008 11:17 AM

AlkontAhmad said:

sorru but i triy it but it didnt work with me i dont know why

# May 27, 2008 6:02 AM

Snives said:

Perhaps, if you need to access some information from the parent, (or really any other control) a better approach might be to pass a reference to that object into your user control.  A simple check for the object type you expect would ensure you can access the object reliably.  You're also not building in dependencies.

# October 22, 2008 1:11 PM

SSchipper said:

I'm with Snives on this one.  Building a dependency between the control and a specific page class seems like a sketchy proposition, but I am looking at a third solution.  I am going to need to pass a particular property to every one of my user controls from everyone of my pages however and I think I may use a hybrid solution by which I reference, not the custom parent page but the custom *base* page that all my pages inherit from.  The controls are then dependent only on the base page which I can guarantee will always be present.  For my custom controls to get an ID from the parent they can call:

((CustomBasePage)this.Parent).MyID

where MyID is defined in CustomBasePage.  

Additionally I now do not have to remember to pass this ID to every instantiation of my user control.

# December 19, 2008 2:49 PM

sri said:

Typecasting helped me a lot to step into the the required control.

Thanks.

# February 17, 2009 5:33 AM

sky said:

sorry,

This is a nother Q:

How can we finde parent page?

Thank u

# March 1, 2009 6:14 AM

DigiMortal said:

sky, you can always use Page property of user control. But it is not suggested way to go. User controls is supposed not to know about its parent container. Parent container user control and if user control has to notify parent container about something then it fires events that parent can handle.

# March 1, 2009 7:32 AM

Seth said:

It makes no sense to me that a user control is not supposed to know about it's parent container.  I'm not well versed in .Net, but lets say I create a control on a page that is going to get the ID of a user from the parent then write out some variables from a DB.  This control would be used on many pages.  I don't come from a .NET background so I have a really hard time understanding it.  It's driving me nuts.

# May 7, 2009 11:16 AM

monster zero said:

cool.  it worked for me

# October 25, 2010 10:28 AM

Kristiijan said:

Well, not always is true: "this situations is a warning sign - there's something wrong with UI structure". I am developing a DotNetNuke module and this hint delped me to get data, needed for nafigation from the parent. | Thaynk you!

# July 1, 2011 4:20 PM

skhan said:

is wrong or is not completed yet.

Have to specify which page is going to be used by @reference directive

# September 26, 2011 6:33 AM