user-controlsdotnetnukedotnetnuke-moduledotnetnuke-5

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


Solution

  • 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