4

In UserControl1 there is a custom event which I want to wire in UserControl2.

in UserControl1 I have declared the custom event as:

 public event MYDelegate SendMessage;

while my delegate defination is in other class library as:

public delegate string MYDelegate(string message);

I am firing SendMessage in my code as below:

  SendMessage(txt.Text);

Kindly guide me how to wire SendMessage() event in UserControl2. My idea was do something like in below example but not sure how to get/ access UserControl1 object in UserControl2.

Please help me.

UserControl1.SendMessge+=ListnerMetod();
  • 1
    Are both usercontrols hosted by the same parent? – Mark Hall Apr 30 '13 at 01:30
  • Sounds like you want to use [MessageBus](http://stackoverflow.com/questions/722675/implementing-a-message-bus-architecture) so it will look like this: http://www.ronaldwidha.net/2010/05/31/a-simple-example-of-the-webformsmvp-cross-presenter-messaging/ – Jeremy Thompson Apr 30 '13 at 01:35

2 Answers2

4

You are almost there. You just need to attach SendMessage to UserControl2's ListnerMetod.

As Mark Hall said, it is not a good practice to fire an event from one control to another without parent page knowing.

Here is the sample code of firing an event through a parent page.

Default.aspx (Parent Page)

<%@ Register Src="SenderUserControl.ascx" TagName="SenderUserControl" 
  TagPrefix="uc1" %>
<%@ Register Src="ReceiverUserControl.ascx" TagName="ReceiverUserControl" 
  TagPrefix="uc2" %>
<uc1:SenderUserControl ID="SenderUserControl1" runat="server" />
<uc2:ReceiverUserControl ID="ReceiverUserControl1" runat="server" />

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SenderUserControl1.SendMessage += m => ReceiverUserControl1.ListnerMethod(m);
    }
}

SenderUserControl.ascx

public delegate void MessageHandler(string message);

public partial class SenderUserControl : System.Web.UI.UserControl
{
    public event MessageHandler SendMessage;

    protected void Button1_Click(object sender, EventArgs e)
    {
        SendMessage("test");
    }
}

ReceiverUserControl.ascx

public partial class ReceiverUserControl : System.Web.UI.UserControl
{
    public void ListnerMethod(string message)
    {

    }
}

Credit to Mark Hall

Win
  • 61,100
  • 13
  • 102
  • 181
2

If both UserControls are hosted by the same parent, attach a handler in the parent to the UserControls event that you want to subscribe to then call a method in the second UserControl in the handler.

Mark Hall
  • 53,938
  • 9
  • 94
  • 111
  • is there no way I can wire it directly user control to user control ? or directly between a master page to user control or content page to master ? – user2279051 Apr 30 '13 at 01:40
  • Is this Asp.net or Winforms? – Mark Hall Apr 30 '13 at 01:43
  • 1
    I work mostly in Winforms or Wpf, you can try something like this to see if it will work. `userControl1.SendMessage += new UserControl1.MyDelegate(userControl2.ListenerMethod);` This is just adding the listener Method as the EventHandler. Make sure the Method has the same return type as the Delegate. – Mark Hall Apr 30 '13 at 01:48
  • @user2279051 I just tested on an ASP.Net Project and it works also. – Mark Hall Apr 30 '13 at 02:06
  • it looks like I m doing something wrong or missing something. Can you please confirm are you doing from usercontrol to usercontrol ? or from usercontrol to masterpage ? If possible can you please share your code. – user2279051 Apr 30 '13 at 03:20
  • 1
    It is not good for an UserControl to have knowledge of another UserControl, I did it in the Page_Load event of the Page they were hosted on. You will need to use the instance names of the UserControls. – Mark Hall Apr 30 '13 at 03:44
  • Thanks @Mark, just last thing to ask you please. You said it is not good for UC to know other UC. Then what is best way to to UC or component communication ? – user2279051 Apr 30 '13 at 06:30
  • The best way is to have the object that hosts the usercontrols handle the communication using public properties, methods and events. – Mark Hall Apr 30 '13 at 07:28