Sometime we need that all our user controls should communicate to each other when they are on a page. The functionality can be achieved by putting a delegate instance on the user control and on the other user control create a target as following (suppose you have a UserControl AddEditUser.ascx and the page on which you put this user control has user listing and you want that whenever a user is added the Grid must be refreshed. So let's do it using aforesaid approach. Below is the code in the UserControl for the AddEditUser.ascx)
And in the aspx page you write following code
A very simple approach
to use but some questions might arise. like
Q : Why ControlBroadCastEventArgs ?
A : This is the broadcast message. You can extend this message like you can add a DateTime type property that holds the timestamp when the broadcast occured.
Q : Why virtual methods ?
A : Nothing special but in case you want your user control must inherit the above BaseUserControl but should not participate in broadcasting or receiving broadcasts then just override this the Register method and write nothing inside. Or same way like if you want that your control only should listen to the outer broadcasts but should not broadcast anything then override the broadcast and put nothing inside.
Now let's take a look what will be inside your user control that you will inherit from the above BaseUserControl
To listen to a broadcast
protected void BroadcastButton_Click(object sender, EventArgs e)
Using the above approach any control can broadcast anytime (means from any event) and other controls can listen to it. Any number of controls can be participants to listen to above broadcast.