DotNetNuke Page Navigator: Recursive Drop Down List

This example shows how to make a recursive drop-down list. It uses the DotNetNuke Tabs table because it is an example of a recursive table.

Download and install from this link: PageAdmin_01.00.00_Install.zip  (note: If using DNN4 install and run LinqPrep first. If using DNN5 follow these directions first)

The thing to note is that even though the list of tabs can be huge there is only one hit to the database. Linq is used to query the collection over and over as the code walks the tree to create the nested list.

(Note: Normally you would not make a module that accesses the DotNetNuke core tables directly because they could change. The core table is only used for demonstration)

The Code

View.ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="View.ascx.cs" Inherits="PageAdmin.View" %>
Select page to navigate to: <asp:DropDownList ID="DDLTabs" runat="server" AutoPostBack="True" 
    onselectedindexchanged="DDLTabs_SelectedIndexChanged">
</asp:DropDownList>

View.ascx.cs:

//
// DotNetNuke® - http://www.dotnetnuke.com
// Copyright (c) 2002-2008
// by DotNetNuke Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using DotNetNuke.Entities.Modules;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq;

namespace PageAdmin
{
    public partial class View : PortalModuleBase
    {
        IQueryable<Tab> EntireTable;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GetEntireTable();
                BindDropDown();
            }
        }

        #region GetEntireTable
        private void GetEntireTable()
        {
            PageAdminDALDataContext PageAdminDALDataContext = new PageAdminDALDataContext();
            // Get the entire table only once
            EntireTable = (from Tabs in PageAdminDALDataContext.Tabs
                           where Tabs.IsSecure == false
                           where Tabs.PortalID == this.PortalId
                           where Tabs.IsDeleted == false
                           select Tabs).ToList().AsQueryable();
        }
        #endregion

        #region BindDropDown
        private void BindDropDown()
        {
            // Create a Collection to hold the final results
            ListItemCollection colListItemCollection = new ListItemCollection();

            // Get the top level
            var results = from Tabs in EntireTable
                          where Tabs.IsSecure == false
                          where Tabs.PortalID == this.PortalId
                          where Tabs.Level == 0
                          where Tabs.IsDeleted == false
                          select Tabs;

            // Loop thru the top level
            foreach (Tab objTab in results)
            {
                // Create a top level item
                ListItem objListItem = new ListItem();
                objListItem.Text = objTab.TabName;
                objListItem.Value = objTab.TabID.ToString();
                // Add a top level item to the final collection
                colListItemCollection.Add(objListItem);

                // Add the children of the top level item
                // Pass the current collection and the current top level item
                AddChildren(colListItemCollection, objTab);
            }

            // Bind the final collection to the drop downs
            DDLTabs.DataSource = colListItemCollection;
            DDLTabs.DataTextField = "Text";
            DDLTabs.DataValueField = "Value";
            DDLTabs.DataBind();

        }
        #endregion

        #region AddChildren
        private void AddChildren(ListItemCollection colListItemCollection, Tab objTab)
        {
            // Get the children of the current item
            // This method may be called from the top level or recuresively by one of the child items
            var ChildResults = from Tabs in EntireTable
                               where Tabs.IsDeleted == false
                               where Tabs.ParentId == objTab.TabID
                               select Tabs;

            // Loop thru each item
            foreach (Tab objChildTab in ChildResults)
            {
                // Create a new list item to add to the collection
                ListItem objChildListItem = new ListItem();
                // AddDots method is used to add the dots to indicate the item is a sub item
                objChildListItem.Text = String.Format("{0}{1}", AddDots(objChildTab.Level), objChildTab.TabName);
                objChildListItem.Value = objChildTab.TabID.ToString();
                colListItemCollection.Add(objChildListItem);

                //Recursively call the AddChildren method adding all children
                AddChildren(colListItemCollection, objChildTab);
            }
        }
        #endregion

        #region AddDots
        private static string AddDots(int intDots)
        {
            String strDots = "";

            for (int i = 0; i < intDots; i++)
            {
                strDots += ". ";
            }

            return strDots;
        }
        #endregion

        #region DDLTabs_SelectedIndexChanged
        protected void DDLTabs_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Get Description of the selected Tab
            PageAdminDALDataContext PageAdminDALDataContext = new PageAdminDALDataContext();
            var TabInfo = (from Tabs in PageAdminDALDataContext.Tabs
                           where Tabs.TabID == Convert.ToInt32(DDLTabs.SelectedValue)
                           select Tabs).FirstOrDefault();

            // Navigate to the page
            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL(TabInfo.TabID));
        }
        #endregion
    }
}

[Back to: The ADefWebserver DotNetNuke HELP WebSite]

 


Buy DotNetNuke Modules from Snowcovered

 DotNetNuke Powered!DotNetNuke is a registered trademark of DotNetNuke Corporation.