Introduction
Here we are going to check how to implement module to module communication in Dnn. Using this we can pass data from one module to another module in the same page
Background
In DNN suppose our page contains 2 different modules , one module is with a dropdown list and the other module has a gridview and we need to bind the gridview based on the dropdown list selected value. In this case we need to pass data from one module to another and we can achieve this by using module to module communication feature in DNN
Using the code
To achieve communication between the modules we need to implement IModuleCommunicator
and IModuleListener
interfaces. Implementing the IModuleCommunicator
interface allows our module to send messages to all the modules in the same page that are listening for IMC messages. Implementing the IModuleListener
interface allows that module to receive all the messages sent from modules on the same page. In order to use these interfaces we need to include following namespace in our code.
using DotNetNuke.Entities.Modules.Communications;
The module from which we sends data is referred as Communicator and the module which receives data is referred as Listener.
In order to create a communicator module we need to implement the IModuleCommunicator
interface. ModuleCommunicationEventArgs
class would be used to share the information with other modules. Also we need to create an instance of the ModuleCommunicationEvenHandler
delegate as an event named ModuleCommunication
as shown below.
public partial class CommunicatorTest : PortalModuleBase , IModuleCommunicator
{
public event ModuleCommunicationEventHandler ModuleCommunication;
private void Page_Load(object sender, System.EventArgs e)
{
try
{
if (!IsPostBack)
{
BindDropdown();
}
}
catch (Exception exc)
{
}
}
protected void ddlYear_SelectedIndexChanged(object sender, EventArgs e)
{
if (ModuleCommunication != null)
{
ModuleCommunicationEventArgs mcArgs = new ModuleCommunicationEventArgs(this.GetType().ToString(), ddlYear.SelectedValue, this.GetType().ToString(), "ListenerTest");
ModuleCommunication(this, mcArgs);
}
}
}
Here we need to pass selected value of dropdown list to the listener module, we have to create an object of ModuleCommunicationEventArgs
class and the value is passed to ModuleCommunicationEventArgs
class constructor and the object of it is passing a parameter to event handler.
In order to receive the information from communicator module we need to implement the IModuleListener
interface in the listener module which needs to implement OnModuleCommunication
method as shown in the below code and we can access the value as shown in the below code
public partial class ListenerTest : PortalModuleBase, IModuleListener
{
protected void Page_Load(object sender, EventArgs e) {
}
public void OnModuleCommunication(object sender, ModuleCommunicationEventArgs e)
{
if (e.Target == "ListenerTest")
{
string selectedYear = e.Value;
}
}
}
Also we can make a Module as both listener and Communicator the same time, in that case we need to implement both the interfaces. Also we can make a communicator module as a listener module of another module and we can create multiple listener modules for a communicator module.