EfficionDave
EfficionDave

Reputation: 2746

Linking buttons of a UserControl to functions on a parent control

I've got a User Control with Save and Cancel buttons that I'm displaying in another user control. When the user hits Save, I want the inner user control to do it's standard functionality but I would also like the parent control to do some things. What's the best way to handle this?

Upvotes: 0

Views: 103

Answers (2)

graham mendick
graham mendick

Reputation: 1839

Bubble Events fit the bill, http://msdn.microsoft.com/en-us/library/aa719644(v=vs.71).aspx

You can raise a bubble event in your user control and it will recursively be passed to each of the user control's ancestors until it's handled

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460208

The best approach is raising a custom save-event that you can handle in the "outer" control.

http://www.codeproject.com/Articles/8797/Mastering-Page-UserControl-Communication#4.3

The advantage of this event-approach is that UserControls remain being reusable. You can use UserControl A in other pages(or UserControls) as well even when they don't handle this event. It's part of the controller to decide what is needed and what should be done.

UserControls as a rule should not depend on specific controllers, otherwise they are hard-linked and not reusable. That would be also a good source for nasty erros. A UserControl might be a controller for other nested (User-)Controls but not for the page itself.

Communication Summary:

  • Page -> UserControl -> public properties and methods
  • UserControl -> Page -> Events
  • UserControl -> UserControl -> the controller-UserControl adopts the page-role(see above, your use-case)

Upvotes: 2

Related Questions