Thursday, March 29, 2012

Passing values between two usercontrols?

Hi

How do I pass values between two UserControls?

Can I just register the one usercontrol inside the other usercontrol and then use a public property?

ThanksThis is a very common question, but there is a good and a bad way to answer it.
The short, bad way is to expose a public property, then have one of the controls find the other using FindControl and then set the property after having cast to the right type.
The problem with this is that it introduces a strong coupling between your two controls. What if one of the controls is not on the page? What if it's been replaced with another control with the same ID? What if one control evolves and not the other?

Which leads to the good way to do it: not to do it. The page or a higher container control should act as a global controller here and manage the relationships between the controls it contains. The controls themselves should be self-contained and not depend on any external control to function. The page should handle the events from the controls and manage the communication between controls from there.

Does this make sense?
Hi

Making sense.

Thanks

0 comments:

Post a Comment