ASP.NET Event delegation between user controls?

To make each control reusable and decoupled from other controls, I've always taken this approach: Each container will know about the controls that it contains one level deep, but not about the container containing it. To communicate up, you are correct in using events. This provides your layer of abstraction and makes all of your controls reusable.

That being said, let's take a look at your example. Your TitleControl does not contain anything, so all it will do is fire events. Your HeaderControl references your TitleControl and handles it's events.

If it doesn't completely handle your TitleControl's events, then have it declare and fire it's own event, and pass along the original sender and event args. At your page level, your page will handle your HeaderControl's events. Since your page is also the container for your TabsControl, call a public method in your TabsControl inside the event handler for your HeaderControl's event and pass in the bubbled up EventArg information.

Basically, use events to bubble things up, and use public methods or properties to push things down.

Thanks for the answer, Aaron! This will work (and follows the same direction that Daniel was moving towards) and it's a perfectly suitable solution. I was wishing/hoping that there would be cleaner method that didn't involve touching HeaderControl, for example a single event that propagated to the Page level (root container) automatically.

– Ishan Mar 31 '10 at 4:41.

It's similar! The main difference is that I don't have access to the TitleControl from the Page level since it's encapsulated inside HeaderControl. This means that I cannot wire up the event handler in the fashion that the article mentions.

From any given Page, I can only directly access HeaderControl. – Ishan Mar 31 '10 at 3:45 I'm pretty sure you solve that by exposing the desired event from the Title page on the HeaderControl and bubble it manually (so to speak). The automatic way to do it is, I think, Routed Events (msdn.microsoft.Com/en-us/library/ms742806.

Aspx) but I'm reasonably sure those are only available in WPF. – R0MANARMY Mar 31 '10 at 3:56.

You could have the Page implement an interface that has the events on them. Then have the tab control see if the page implents the desired interface. If it does then hook up to the events.

Missed the part about the title control. The title control would also have to make itself known to the Page via an interface method. You could then hook up the events at some point (perhaps the load event).

This would of course only work if the controls where added to the page prior to the load event.

The HeaderControl can expose an event that gets consumed by the TabsControl. I've answered a similar question before (with a sample project attached).

To make each control reusable and decoupled from other controls, I've always taken this approach: Each container will know about the controls that it contains one level deep, but not about the container containing it. To communicate up, you are correct in using events. This provides your layer of abstraction and makes all of your controls reusable.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions