Click here to Skip to main content
15,886,110 members
Articles / Dotnet nuke
Tip/Trick

Module to Module communication in DotnetNuke

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
15 Nov 2019CPOL2 min read 3K   1   1

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.

C++
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.

C++
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

ASP.NET
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionTIP Pin
Nelek15-Nov-19 3:53
protectorNelek15-Nov-19 3:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.