[Back]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Collections;
using System.Web.UI.WebControls;

namespace ADefWebserver.Modules.CategoryAdmin
{
    public class CategoriesDropDown
    {
        private IQueryable<AdefWebserverCategory> EntireTable;

        public CategoriesDropDown()
        {
            GetEntireTable();
        }

        #region GetEntireTable
        private void GetEntireTable()
        {
            // Get the entire table only once from cache
            EntireTable = CategoriesTable.GetCategoriesTable();
        }
        #endregion

        #region Categories
        public ListItemCollection Categories(int BranchNotToShow)
        {
            // Create a Collection to hold the final results
            ListItemCollection colListItemCollection = new ListItemCollection();

            // Get the top level
            var results = from AdefWebserverCategories in EntireTable
                          where AdefWebserverCategories.Level == 1
                          where AdefWebserverCategories.CategoryID != BranchNotToShow
                          orderby AdefWebserverCategories.CategoryName
                          select AdefWebserverCategories;

            // Create a none item
            ListItem objDefaultListItem = new ListItem();
            objDefaultListItem.Text = "[None]";
            objDefaultListItem.Value = "0";
            colListItemCollection.Add(objDefaultListItem);

            // Loop thru the top level
            foreach (AdefWebserverCategory objAdefWebserverCategories in results)
            {
                // Create a top level item
                ListItem objListItem = new ListItem();
                objListItem.Text = objAdefWebserverCategories.CategoryName;
                objListItem.Value = objAdefWebserverCategories.CategoryID.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, objAdefWebserverCategories, BranchNotToShow);
            }

            return colListItemCollection;
        }
        #endregion

        #region AddChildren
        private void AddChildren(ListItemCollection colListItemCollection, AdefWebserverCategory objAdefWebserverCategory, int BranchNotToShow)
        {

            // 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 AdefWebserverCategories in EntireTable
                               where AdefWebserverCategories.ParentCategoryID == objAdefWebserverCategory.CategoryID
                               where AdefWebserverCategories.CategoryID != BranchNotToShow
                               select AdefWebserverCategories;

            // Loop thru each item
            foreach (AdefWebserverCategory objCategory 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(objCategory.Level), objCategory.CategoryName);
                objChildListItem.Value = objCategory.CategoryID.ToString();
                colListItemCollection.Add(objChildListItem);

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

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

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

            return strDots;
        }
        #endregion
    }
}


Buy DotNetNuke Modules from Snowcovered

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