First time at my blog? Check out the table of contents! x
posted on Sunday, June 08, 2008 2:49 PM | Filed Under [ DotNetNuke Open Source ]

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>

 

Comments Leave Yours...
baldwin
6/8/2008 7:42 PM
# re: Programmatically Change the Visibility of a DotNetNuke Module

Thanks a lot. That is what I exactly desired.You had done a great job for sharing your tip!However,will your way effact the page performanece? for example, if the "ToggleVisibilityCS" exists some bind data control, it also wwill execute the DataBind Process. Is is corret? I also notice the module had the "isDeleted" property to determine whether insert the module insert the page,will that way be the better?

Hans Kruse
9/2/2008 1:37 PM
# re: Programmatically Change the Visibility of a DotNetNuke Module

You are not the only one who needs to hide a module instance.

This is very useful to hide a control based on a users role.

Narong
11/10/2008 10:40 PM
# re: Programmatically Change the Visibility of a DotNetNuke Module

A big thanks !!

kerryduan
5/27/2009 11:49 PM
# re: Programmatically Change the Visibility of a DotNetNuke Module

Great tip! Checking for IsEditable to determining whether to hide or show the module is a good method! i never thought of that.
Thanks alot!

Arjan Rijkens
8/24/2009 2:54 AM
# re: Programmatically Change the Visibility of a DotNetNuke Module

Nice article!

Do you know how to hide/show other modules than the module running the code?

I have a custom module that is located in a page with other modules. Depending on some conditions I want to programmatically hide/show the other modules in my custom module.

Any idea?

Ryan
10/8/2009 2:22 PM
# re: Programmatically Change the Visibility of a DotNetNuke Module

Thanks! I've been struggling with exactly this all day. I had a working solution but yours just knocked my code down from about 20 lines to 5. Thanks

Post Your Comment

Title
Required
Name
Required
Email
Optional
Url
Optional
Comment  
Please add 2 and 4 and type the answer here:

Who Is Rafe

rafe

Rafe Kemmis

I am an audacious web developer with a double bachelor of science in Computer Science and Mathematics. I specialize in Microsoft ASP.Net, Silverlight, and Adobe ActionScript.

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