Inter module commucnication is used when you want to communicate your module with each other. For example, you have 2 modules. In first module you want to select some value from drop down and in the 2nd module you would like to publish data on the label as per the selected value in the drop down.
But dropdown and grid both are in different module, to communicate module we can use inter module communication.
How IMC Works
IMC functionality is divided into two parts: Communicating (sending messages) and Listening (receiving messages). In order to add one or both of these functionalities to a modules, you must have the module class implement the respective IMC interfaces which the DNN Framework.
The IModuleCommunicator and IModuleListener Interfaces
Both IModuleCommunicator and IModuleListener live inside the DotNetNuke.Entities.Modules.Communications namespace, so you may find it convenient to reference these namespaces at the top of your code.
Implementing the IModuleCommunicator interface allows your module to dispatch messages to all the modules on the page that are listening for IMC messages. On the flip side of the equation, implementing the IModuleListener interface allows your module to receive all the messages sent from modules on the same page.
Implementing IModuleCommunicator
To implement IModuleCommunicator all you need to do is define an instance of the ModuleCommunicationEvenHandler delegate as an event named ModuleCommunication within your class.
C# Example – Module 1 (Communicator) -> ViewCommunicator.ascx.cs
using System;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Communications;
public partial class ViewCommunicatorCS : PortalModuleBase,IModuleCommunicator
{
public event ModuleCommunicationEventHandler ModuleCommunication;
}
Creating the Event Args Object
When you raise the ModuleCommunication event you are required to pass in an instance of the ModuleCommunicationEventArgsDotNetNuke class which is found in the Entities.Modules.Communications namespace. This class is like an envelope that you use to send IMC messages out to other modules. You need to instantiate a copy and set its properties before you pass it to the ModuleCommunication call.
Write the below code into the dropdown selectedindexchanged event or you can write the code as per your module requirements.
// the IMC message data gets stored inside
// a ModuleCommunicationEventArgs object
ModuleCommunicationEventArgs mcArgs =
new ModuleCommunicationEventArgs();
mcArgs.Target = "Arbitrary Text";
mcArgs.Text = "Your payload text";
mcArgs.Type = "Your custom type";
mcArgs.Value = ddlSelect.SelectIndex.Item
// if ModuleCommunication is null,
// the cache settings for your module
// might need to be set to 0 (turned off)
if (ModuleCommunication != null)
{
// calling your ModuleCommunication delegate event
// will cause the event to be raised
ModuleCommunication(this, mcArgs);
}
Implementing IModuleListener
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.
Listener Module -> ViewListener.ascx
<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="ViewListener.ascx .cs"
Inherits="ViewListener" %>
ViewListener.ascx.cs
using System;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Communications;
public partial class SampleListenerCS :
PortalModuleBase, IModuleListener
{
public void OnModuleCommunication(object s,
ModuleCommunicationEventArgs e)
{
throw new NotImplementedException();
}
}
Add below code at Page_Load event
public partial class ViewListener :
PortalModuleBase, IModuleListener
{
public void OnModuleCommunication(object s,
ModuleCommunicationEventArgs e)
{
if (e.Target == "Pizza")
{
Label1.Text = "Garlic Detected In Pizza";
}
if (e.Target == "Bread")
{
Label1.Text = "Bread Detected In Pizza";
}
}
}
Configure both modules and place on the same page. By changing the dropdownlist values will displayed the data in Listener module respectively.