June 2008 Entries

It turns out that I am not the only person that has ever needed to conditionally hide or show a DotNetNuke module instance. Reader Baldwin emailed me wanting to know how I handle the situation where a module instance needs to be removed from a page, but still persist in the database. Here I will show you exactly that.

PortalModuleBase.ContainerControl

As you might recall, all DotNetNuke modules inherit from the PortalModuleBase class, which is found in the DotNetNuke.Entities.Modules namespace. One of the properties that your module inherits is ContainerControl. The ContainerControl property has a getter method that finds and returns the Control that contains your module's instance. In particular, it finds the control named "ctrYourModuleID", where "YourModuleID" is a unique identifier for the module instance:

 

Public ReadOnly Property ContainerControl() As Control
    Get
        Return FindControlRecursive(Me, "ctr" & ModuleId.ToString)
    End Get
End Property

 

The Control that ContainerControl gives you is actually the <div> that renders around your container when the page is sent to the browser. Here is an example:

 

image

 

ContainerControl.Visible

Since this is a regular Control, it has a Visible property which when assigned a value of false will prevent the Control, and all of its children controls from rendering out to the browser. In other words, hiding your module instance is as simple as setting the Visible property on its ContainerControl to false. Just insert this line in the appropriate location of your module code:

ContainerControl.Visible = false;

 

That was simple!

Working Example:

Here is quick module that I cooked up to demonstrate this. The following module is visible when the module is in edit mode, but is hidden when the module is in regular view mode. To switch between these modes you must be logged in as an administrator and then use the mode radio buttons in the top control panel:

image

Files:

ToggleVisibilityCS.ascx.cs - C# code-behind for the module

using System;
using System.Web.UI;

using DotNetNuke.Entities.Modules;

namespace Kemmis.Examples.DotNetNuke
{
    public partial class ToggleVisibilityCS : PortalModuleBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (!IsEditable)
                {
                    // hide module + container when not in edit mode
                    ContainerControl.Visible = false;
                }
            }
        }
    }
}

ToggleVisibilityCS.ascx - xml for the module

<%@ Control Language="C#" AutoEventWireup="true" 
    CodeFile="ToggleVisibilityCS.ascx.cs"
    Inherits="Kemmis.Examples.DotNetNuke.ToggleVisibilityCS" %>
<h2>
    This module is visible.</h2>
<p>
    Switch to View mode in the control panel to make this module invisible.</p>

 

Who Is Rafe

rafe

I Am Rafe Kemmis

I am an audacious entrepreneur with a double bachelor of science in Computer Science and Mathematics. I specialize in Microsoft ASP.Net web application development as well as accredited information systems auditing.

Questions?

Always a thoughtful response. You may post your question on an article, or contact me directly.

Hire Me.

I provide custom solutions to complex problems. I can help your business no matter how large or small.

Contact me now.

Subscribe