Retrieving a property value in a Parent User control from a Child control

I have a ChildUserControl that loads inside ParentUserControl. The host page loads the ParentUserControl.

I need to access properties in ParentUserControl from ChildUserControl.

thank you for your time

+4
source share
7 answers

Platform / language, etc. it is not clear, therefore this answer is necessarily foggy ... however, in the general case, the child control cannot access the properties of the parent (directly), since many different types of parent controls can host control. The child should not be rigidly attached to one parent, otherwise he may be part of the parent.

Typically, you can try to simply remove this requirement - it sounds like an odd design. However, some frameworks support something like this - for example, dependency properties in WPF.

An interface-based design (for the parent (s)) is one approach, but it is not very clean. For .NET events, etc. - Another common way for a child to communicate with a parent - the child provides events that different parents can consume in different ways.

In addition, you enter the parent testing / casting area (either against the class or interface) to access the details from the parent - for example:

ParentControlType parent = this.Parent as ParentControlType; if(parent != null) { // access properties etc of "parent" } 

(you can also use the interface here, still a bit hacked anyway ...)

Personally, instead of using this type of cast from the child, I would rather have the parent take control; the parent sets properties for the child and listens for events (and not the properties of the child set for the parent):

 // in the parent child.SomeProperty = 132; child.SomePropertyChangd += delegate { // do something at the parent }; 

Thus, the child does not know or care about the parent. All he knows is that he has properties (etc.) and can notify other people about interesting changes.

+10
source

Access to the parent element is possible from the child control via ((ParentType) Me.Parent). This is not recommended. There is no way to know that the control will not be used on another page / control. The compiler would not catch this; this cast would throw an exception at runtime.

The best way to deal with this is to provide the parent with any information needed to manage the child (create public properties or methods in the controls). It may also mean that the parent control provides a strongly typed reference to the child control (possibly through an interface). Thus, the compiler ensures that you will not have a case where the child control accepts a parent type of the wrong type.

+1
source

Sorry for what you did not understand. The platform is .NET, and the language is C #.

I have one parent element that contains several child controls. This was one way to achieve visual inheritance in C #, as there were several user interface controls that were common among children.

By interface design, do you propose that both parent and child implement a common interface?

0
source

You can use Reflection as follows:

 public static T GetData<T>(this Control c, string propertyName, T defaultValue){ Type t = obj.GetType(); var prop = t.GetProperties().FirstOrDefault(p => p.Name == propertyName); if(prop == null) return defaultValue; else { object val = prop.GetValue(obj, null); if(val == null) return defaultValue; else return (T)val; } } 

This is a simple extension method that extends "Control", but you can easily put it on anything (I used it on an object when I was passing anonymous classes, which in itself is a bad idea!)

Then use will be:

 string someData = this.Parent.GetData("someProperty", string.Empty); 
0
source

You can use the this.Parent property of the child control, but if you are looking for a more suitable OO solution, you would not. Allowing the child to learn something about the parent ultimately binds them and limits reuse. In most cases, this is only of interest to the theory, and if you beat some kind of quick one-time site, you can violate it as necessary. But a better way is to use event-based messages.

0
source

Thanks for answers.

I will review Reflection. However, I heard that he has a performance bottleneck.

Jim - you say:

~~

The best way to deal with this is to provide the parent with any information needed to manage the child (create public properties or methods in the controls). It may also mean that the parent control provides a strongly typed reference to the child control (possibly through an interface). Thus, the compiler ensures that you will not have a case where the child control accepts a parent type of the wrong type.

~~

In my situation, both the parent and child (ren) implement the same interface. Is this a bad design? If I add the property of the parent link to the interface, will it work, given that the parent implements the same interface?

0
source

If you have two nested user controls (UC):

UC1.ascx
| - UC2.ascx

In UC1.ascx, you define a public function or property that does what you need:
fnUC1 (pairs)

In UC2.ascx, you get this function through this code:

CType (parent.parent, UC1) .fnUC1 (pairs)

If your UC2 control is inside another container (for example, a repeater), you will also have to exit this container, and therefore the code will be:

CType (Parent.Parent.Parent, UC1) .fnUC1 (pairs)

I admit that this is not a β€œstate of the art” ... but it works for nested controls that always work together, such as displaying parent (UC1) / child (UC2).

Hope this will be helpful.

Pierre.

0
source

All Articles