Communicating (sending messages)
Listening (receiving messages).
In order to add one or both of these functionalities to a modules, we must have the module class implement the respective IMC interfaces which the DNN Framework has defined for us. Implementing the IModuleCommunicator interface allows our module to dispatch messages to all the modules on the page that are listening for IMC messages. Implementing the IModuleListener interface allows our module to receive all the messages sent from modules on the same page. Both IModuleCommunicator and IModuleListener live inside the DotNetNuke.Entities.Modules.Communications namespace. Hence, we have to include this namespace at the top of our code. like:
using DotNetNuke.Entities.Modules.Communications;
CommunicatorModule Class
To implement IModuleCommunicator all we need to do is define an instance of the ModuleCommunicationEvenHandler delegate as an event named ModuleCommunication within our Communicator class. like:
public partial class CommunicatorModule : PortalModuleBase, IModuleCommunicator { public event ModuleCommunicationEventHandler ModuleCommunication; } To raise the ModuleCommunication event on button click, we have to create an object of ModuleCommunicationEventArgs class and set data in it. like: public partial class CommunicatorModule : PortalModuleBase, IModuleCommunicator { public event ModuleCommunicationEventHandler ModuleCommunication; void Button1_Click(object sender, EventArgs e) { try { if (ModuleCommunication != null) { ModuleCommunicationEventArgs mcArgs = new ModuleCommunicationEventArgs(); mcArgs.Sender = this.GetType().ToString(); mcArgs.Target = "ListenerModule"; mcArgs.Type = this.GetType().ToString(); mcArgs.Value = "Your Name"; //can be any object type, like: DataSet mcArgs.Text = "Example"; ModuleCommunication(this, mcArgs); } else { this.Controls.Add(new LiteralControl("Can't find IMC Listener")); } } catch (Exception exc) { DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc); } } } We can send any object type data in value field. We have to cast it to it's proper type at the time of catching it in the Listener class. ListenerModule Class In order for your module to receive IMC messages it must implement the IModuleListener interface. This interface requires your class to have a method named OnModuleCommunication which takes two parameters, and Object type and a ModuleCommunicationEventArgs type, respectively. like: partial class ListenerModule : PortalModuleBase, IModuleListener { public void OnModuleCommunication(object sender, ModuleCommunicationEventArgs e) { if (e.Target == "ListenerModule") { string strName = e.Value.ToString(); //cast it to sent object type and assign. like: //Dataset ds = (DataSet)e.Value; string strText = e.Text; } }