Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DNN 5 - Can't get current ModuleId from nested user control in custom module

I'm writing a custom module for DNN 5, and I need a "Manage" link to be on every control in the module. I created a new UserControl ("ManagerLink") that inherits from PortalModuleBase, put my link into that control, and dropped that control on ALL OF MY MAIN CONTROLS.

The problem is that ModuleId and TabId are always -1 in "ManagerLink" nested control. PortalId works just fine, and I can get a TabId by doing PortalSettings.ActiveTab.TabID.

  1. Why can't I get ModuleId and TabId in from "ManagerLink" control, even though it inherits from PortalModuleBase?

  2. Is there an alternative method to get ModuleId (equivalent of PortalSettings.ActiveTab.TabID)

UPDATE 2014:

Just saw another answer that's way better than the original (and accepted it).

If you're using DNN 6 and earlier, replace ModuleBase with PortalModuleBase

like image 756
roman m Avatar asked Mar 13 '09 21:03

roman m


2 Answers

William Severance from DNN forum answered this one for me, I'll post the answer here as well.

Since the child control inherits from PortalModuleBase, I would do the following in the Page_Load handler of the parent control

Note: ManagerLink is assumed to be a reference to the child control

VB.NET:

With ManagerLink
    .ModuleConfiguration = Me.ModuleConfiguration
    .LocalResourceFile = Me.LocalResourceFile
End With
C#:
protected void Page_Load(System.Object sender, System.EventArgs e)
{
    ManagerLink.ModuleConfiguration = this.ModuleConfiguration;
    ManagerLink.LocalResourceFile = this.LocalResourceFile
}

The above allows the child control to use the parent's ModuleConfiguration (which will include ModuleId) and LocalResourceFile for any localization.

like image 91
roman m Avatar answered Sep 26 '22 15:09

roman m


I just wanted to add my 2 cents here, using the answer of @roman-m and extending on it,

I was able to do it in the nested control itself like so:

//fires first in the sequence, calling initialise components
override protected void OnInit(EventArgs e)
{
    InitializeComponent();
    base.OnInit(e);
}

private void InitializeComponent()
{
    this.Load += new System.EventHandler(this.Page_Load);
    //this binds a handler to the parent's init event
    this.Parent.Init += new EventHandler(this.Parent_Init);
}
//the handler gets called, at this point we can cast the parent as a module base
//and load the configuration and resource file into the nested control
private void Parent_Init(object sender, System.EventArgs e)
{
    this.ModuleConfiguration = ((ModuleBase)this.Parent).ModuleConfiguration;
    this.LocalResourceFile = ((ModuleBase)this.Parent).LocalResourceFile;
}

This means that in the Page_Load event of the nested control it will already have the configuration and local resource file on hand.

It also means you don't have to load the configuration and local resource file in on every parent control which uses the child control.

This will only work when the parent is of type ModuleBase of course

And to be even more specific, this works in version 7.00.06

like image 34
m.t.bennett Avatar answered Sep 26 '22 15:09

m.t.bennett