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

13 Comments

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

  • 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.

  • good,Control.Parent ,

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

  • 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.

  • 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.

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

    Thanks.

  • sorry,
    This is a nother Q:
    How can we finde parent page?

    Thank u

  • 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.

  • 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.

  • cool. it worked for me

  • 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!

  • is wrong or is not completed yet.

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

Comments have been disabled for this content.