Skip to main content

Dotnetnuke Inter-module communication (IMC) simplified on version 06.00.02 with c#

Few days ago I developed a module in which I used IMC which really interested me so I decided to write about it. I will try to explain in this post everything that is necessary to make IMC work in the modules.

What is Inter Module Communication?

As the name implies if you want to communicate or in other words send data from one module to another IMC is one way of doing it.

I will be using module A and module B as the names in my post.

An Observation:

One thing I observed while playing around with it that if module A is on page 1 and module B is on page 2 then the data doesn't get passed. If both the modules are on the same page then only the data get passed.

Example:

The basic exercise that I will be performing is to take input from the textbox in Module A and display it in label in Module B.

1) You will be using IModuleCommunicator and IModuleListener interfaces to make this communication works. You will be implementing IModuleCommunicator in the class in the module you want to send the data from and IModuleListener will be implemented in the class in the module in which you want to receive the data.

2) You can have multiple listeners as well . if this is the case then every listener will act as a receiver for the data the communicator module is sending.

3) You will be using ModuleCommunicationEventArgs class to send the data from communicator module class to the Listener module class.

4) DotNetNuke.Entities.Modules.Communications is the namespace that is used in the process.

Module A (Communicator)  
A.ascx

<asp:TextBox ID="txtdisplay" runat="server"></asp:TextBox>
<asp:Button ID="btnclick" runat="server" Text="Button" OnClick="btnclick_Click" />


A.ascx.cs
   using DotNetNuke;   
   using DotNetNuke.Common.Utilities;
   using DotNetNuke.Entities.Modules;
   using DotNetNuke.Entities.Modules;

   using DotNetNuke.Entities.Modules.Actions;
   using DotNetNuke.Security;
   using DotNetNuke.Services.Exceptions;
   using DotNetNuke.Services.Localization;
   using DotNetNuke.Entities.Modules.Communications;


   partial class A : PortalModuleBase,IModuleCommunicator
   {
    public event ModuleCommunicationEventHandler ModuleCommunication;

    protected void btnclick_Click(object sender, EventArgs e)
    {
       try
       {
          ModuleCommunicationEventArgs MCArgs = new ModuleCommunicationEventArgs();
          MCArgs.Sender = "anyname";
          MCArgs.Target = "anything to send in the target to be used as u wish";
          MCArgs.Text =  txtdisplay.Text;
          MCArgs.Value = txtdisplay.Text;

             if (ModuleCommunication != null)
             {
                  ModuleCommunication(this, MCArgs);
             }
       }
           // other implementation
       catch (Exception exc) //Module failed to load
       {
             Exceptions.ProcessModuleLoadException(this, exc);
       }
    }
   }

Module B (Listener) 
B.ascx
<label id="lbldisplay" runat="server" text="Label"></label>
B.ascx.cs
   using DotNetNuke;
   using DotNetNuke.Common.Utilities;
   using DotNetNuke.Entities.Modules;
   using DotNetNuke.Entities.Modules;

   using DotNetNuke.Entities.Modules.Actions;
   using DotNetNuke.Security;
   using DotNetNuke.Services.Exceptions;
   using DotNetNuke.Services.Localization;
   using DotNetNuke.Entities.Modules.Communications;

   partial class B: PortalModuleBase, IModuleListener
   {
         public void OnModuleCommunication(object s, ModuleCommunicationEventArgs e)
         {
            if (e.Sender == "anyname")
            {
                lbldisplay.Text= (string)e.Value;
            }
          }
           // other implementation
   }

Comments

  1. i laready tying above code but why ModuleCommunication always set to null.

    Could you advise?

    ReplyDelete
  2. It is very straight forward code to work with. The only explanation I can think of that it is not finding any listener on the same page. Both the modules should be on the same page to make it work.

    ReplyDelete
  3. Appreciate for your reply.

    Could you advise what the meaning with one page, my schenario is IModuleCommunicator in b.ascx,
    IModuleListener in a.ascx (first i call b.ascx with editurl with popup frm a.ascx) and after b.ascx closed i want to bring information to a.ascx.

    Regards

    ReplyDelete
  4. One page means that both module a and b should be on the same page1(one module can be in content pane and other may be in left pane). you cannot have module a on page 1 and module b on page2. it doesn't work that way as i have pointed out in the observation section in my post.

    I will be glad to be more helpful if you are more clearer in mentioning your scenario.

    ReplyDelete
  5. Is there a way to have IMC working for different pages.

    ReplyDelete
  6. I want to pass data from an html module to another html module in the same page. Where does this code go? I am new to DNN, does this code go in the html module itself? or do I need to access the server?

    ReplyDelete

Post a Comment

Popular posts from this blog

Asp.net mvc razor render partial view using ajax helper

This is the extension to my blog in which I demonstrated rendering of the partial view using jquery Ajax . I want to demonstrate here yet another way by which partial view can be rendered without page refresh. Here is the implementation. Step 1: I will again be using DisplayData class in my demo. Here is it. public class DisplayData { public int ID { get; set; } public DisplayData(int ID) { this.ID = ID; } } Step 2: Create a PartialDemo page @model IEnumerable<MvcApplication5.Models.DisplayData> @{ ViewBag.Title = "PartialDemo"; } @Ajax.ActionLink("Click 1", "PartialDemo", "PartialDemo", new {Data= "1" }, new AjaxOptions { UpdateTargetId = "rsvpmsg" }) @Ajax.ActionLink("Click 2", "PartialDemo", "PartialDemo", new {Data= "2" }, new AjaxOptions { UpdateTargetId = "rsvpmsg" }) <div id="rsvpms

Asp.net mvc razor render partial view using jquery Ajax

I will going to demonstrate how we can render PartialViews using Jquery Ajax. I will be clicking an a href link ,then I will be calling the controller through jquery Ajax which will fill the partialview for a really nice user experience. Step 1: First of all we will be creating an DisplayData class for the use for this example in the model. public class DisplayData { public int ID { get; set; } public DisplayData(int ID) { this.ID = ID; } } Step 2: We will create a Clicks page and write the following code on it. Specially note empty here which will going to empty and then fill partialview with new records. $(document).ready(function () { $('.msg').click(function () { var id = this.id; $.ajax({ url: "/Category/Display", data: { data: id }, success: function (mydata) { $("#link").empty().appe

Asp.net mvc DataAnnotation ValidateAttribute two properties comparison.

Using Datannotion is great but there are scenarious in which the current attributes compare, range etc becomes inadequate especially for the comparisions. So we create here our own custom validation using ValidationAttribute class which is the base class for all the annotation attributes. So by deriving from it and overriding the Isvalid method we can create our custom attribute for the model. So here is the scenario in which I will be validating the Username against the password which should not be equal. Compare attribute cannot be used in this scenario so I have created a custom attribute for that. Here is the model with the attribute. User View Model [CompareUserPass("UserName", "Password", ErrorMessage = "UserName and password cannot be equal")] public class UserView { [Required(ErrorMessage = "UserName Required")] public string UserName { get; set; } [Required(