Vincular botones de un UserControl a funciones en un control principal

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?

preguntado el 12 de junio de 12 a las 19:06

The best approach is raising a custom ahorrar-event that you can handle in the "outer" control. codeproject.com/Artículos/8797/… -

You could have the parent implement an interface with the functionality you want, and in your user control have a property of that interface to reference the parent page. Then from within your child method, you can call your property that references the parent and call the method. -

2 Respuestas

The best approach is raising a custom ahorrar-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 como regla no debería depender de controladores específicos, de lo contrario, están vinculados y no son reutilizables. Eso también sería una buena fuente de errores desagradables. Un UserControl puede ser un controlador para otros controles anidados (User-) pero no para la página en sí.

Resumen de comunicación:

  • Página -> UserControl -> propiedades y métodos públicos
  • UserControl -> Página -> Eventos
  • UserControl -> UserControl -> the controller-UserControl adopts the page-role(see above, your use-case)

Respondido el 12 de junio de 12 a las 19:06

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

Respondido el 12 de junio de 12 a las 19:06

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.