DotNetNuke Module Control Options

You could use a Multi-View inside of your Edit.ascx to create a dynamic way to toggle visible form/content objects and their code within a single Web User Control ascx and it’s ascx.cs code file. This is a popular approach when working inside the Edit control since dynamically loaded sub-controls will not have persisted postback data.

You could also add a new parent ascx file all together:

Add a New Web User Control

References:
Copy Edit.ascx code behind using ... to overwrite the new code behind using ...

Replace class inheritence
 : System.Web.UI.UserControl
With
 : [MyModuleName]ModuleBase

EXAMPLE
public partial class EditMyAsset : System.Web.UI.UserControl

public partial class EditMyAsset : MyModuleNameModuleBase

In App_LocalResources
Copy Edit.ascx.resx To EditMyAsset.ascx.resx

Add Module Definition:
- Open Edit Extension Form
  Host | Extensions | MyModuleName > Edit Extension
- Navigate to
  Module Definitions | Add Module Control

  Add a Key "EditMyAsset"
  Select the Source path "DesktopModules/MyModuleName/EditMyAsset.ascx"
  Select Type: "Edit"

- Open MyModuleName.dnn
  Copy the node where controlKey = "Edit" //moduleDefinitions/moduleControls/moduleControl
  Change the controlKey in the new node to "EditMyAsset"
  Change the controlSrc in the new node to "DesktopModules/MyModuleName/EditMyAsset.ascx"

Dynamically Loading Web User Controls

If this is View Control that will not have form postbacks, then it is okay to dynamically load the Web User Controls.

aspx code
<%@ Control language="C#" Inherits="DotNetNuke.Modules.MyModuleName.View" AutoEventWireup="false"  Codebehind="View.ascx.cs" %>

<asp:PlaceHolder id="phControls" runat="server"></asp:PlaceHolder>
aspx.cs code
using System;
using DotNetNuke.Common;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Security;


namespace DotNetNuke.Modules.MyModuleName
{

    /// -----------------------------------------------------------------------------
    /// 
    /// The ViewMyModuleName class displays the content
    /// 
    /// -----------------------------------------------------------------------------
    public partial class View : MyModuleNameModuleBase, IActionable
    {

        #region Event Handlers

        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
            DotNetNuke.Framework.jQuery.RequestDnnPluginsRegistration();
        }

        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);
        }


        /// -----------------------------------------------------------------------------
        /// 
        /// Page_Load runs when the control is loaded
        /// 
        /// -----------------------------------------------------------------------------
        private void Page_Load(object sender, System.EventArgs e)
        {
            try
            {
                //load the User Controls based on QueryString params
                string query = Request.QueryString["q"];

                switch (query)
                {
                    case "album-grid":
                        SetControl("Controls/AlbumGrid.ascx");
                        break;
                    case "artist-grid":
                        SetControl("Controls/ArtistGrid.ascx");
                        break;
                    default:
                        SetControl("Controls/Default.ascx");
                        break;
                }
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }

        }

        private void SetControl(string ControlPath)
        {
            PortalModuleBase b = (PortalModuleBase)LoadControl(ControlPath);
            b.ModuleConfiguration = this.ModuleConfiguration;
            b.ID = System.IO.Path.GetFileNameWithoutExtension(ControlPath);
            phControls.Controls.Add(b);
        }

        #endregion

        #region Optional Interfaces

        public ModuleActionCollection ModuleActions
        {
            get
            {
                ModuleActionCollection Actions = new ModuleActionCollection();
                Actions.Add(GetNextActionID(), Localization.GetString("EditModule", this.LocalResourceFile), "", "", "", EditUrl(), false, SecurityAccessLevel.Edit, true, false);
                return Actions;
            }
        }

        #endregion

    }

}

Additional Resources

comments powered by Disqus