[Back]

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

namespace ADefWebserver.Modules.CategoryAdmin
{
    public class CatagoriesTree : HierarchicalDataSourceControl, IHierarchicalDataSource
    {
        public CatagoriesTree()
            : base()
        {
        }

        // Return a strongly typed view for the current data source control.
        private CatagoriesView view = null;

        #region GetHierarchicalView
        protected override HierarchicalDataSourceView GetHierarchicalView(string viewPath)
        {
            if (null == view)
            {
                view = new CatagoriesView(viewPath);
            }
            return view;
        }
        #endregion

        #region CreateControlCollection
        // The DataSource can be used declaratively. To enable
        // declarative use, override the default implementation of
        // CreateControlCollection to return a ControlCollection that
        // you can add to.
        protected override ControlCollection CreateControlCollection()
        {
            return new ControlCollection(this);
        }
        #endregion
    }

    #region CatagoriesView
    public class CatagoriesView : HierarchicalDataSourceView
    {
        private string _viewPath;
        public CatagoriesView(string viewPath)
        {
            // This implementation of HierarchicalDataSourceView does not
            // use the viewPath parameter but other implementations
            // could make use of it for retrieving values.
            _viewPath = viewPath;
        }

        #region Select
        // Starting with the rootNode, recursively build a list of
        // nodes, create objects, add them all to the collection,
        // and return the list.
        public override IHierarchicalEnumerable Select()
        {
            CatagoriesEnumerable CHE = new CatagoriesEnumerable();

            // Get the top level
            var results = from AdefWebserverCategories in CategoriesTable.GetCategoriesTable()
                          where AdefWebserverCategories.Level == 1
                          orderby AdefWebserverCategories.CategoryName
                          select AdefWebserverCategories;

            // Loop thru the top level
            foreach (AdefWebserverCategory objAdefWebserverCategory in results)
            {
                // Create a top level item
                ListItem objListItem = new ListItem();
                objListItem.Text = objAdefWebserverCategory.CategoryName;
                objListItem.Value = objAdefWebserverCategory.CategoryID.ToString();
                // Add a top level item to the final collection
                CHE.Add(new CatagoriesHierarchyData(objListItem));
            }

            return CHE;
        }
        #endregion

        #region CatagoriesEnumerable
        // A collection of CatagoriesHierarchyData objects
        public class CatagoriesEnumerable : ArrayList, IHierarchicalEnumerable
        {
            public CatagoriesEnumerable()
                : base()
            {
            }

            public IHierarchyData GetHierarchyData(object enumeratedItem)
            {
                return enumeratedItem as IHierarchyData;
            }
        }
        #endregion

        #region CatagoriesHierarchyData
        public class CatagoriesHierarchyData : IHierarchyData
        {
            public CatagoriesHierarchyData(ListItem obj)
            {
                objListItem = obj;
            }

            private ListItem objListItem = new ListItem();

            public override string ToString()
            {
                return objListItem.Text;
            }

            #region HasChildren
            // IHierarchyData implementation.
            public bool HasChildren
            {
                get
                {
                    // Get the children of the current item
                    var ChildResults = from AdefWebserverCategories in CategoriesTable.GetCategoriesTable()
                                       where AdefWebserverCategories.ParentCategoryID == Convert.ToInt32(objListItem.Value)
                                       select AdefWebserverCategories;

                    return ChildResults.Count() > 0;
                }
            }
            #endregion

            #region string Path
            public string Path
            {
                get
                {
                    return objListItem.Value;
                }
            }
            #endregion

            #region object Item
            public object Item
            {
                get
                {
                    return objListItem;
                }
            }
            #endregion

            #region string Type
            public string Type
            {
                get
                {
                    return "ListItem";
                }
            }
            #endregion

            #region string Text
            public string Text
            {
                get
                {
                    return objListItem.Text;
                }
            }
            #endregion

            #region string Value
            public string Value
            {
                get
                {
                    return objListItem.Value;
                }
            }
            #endregion

            #region GetChildren
            public IHierarchicalEnumerable GetChildren()
            {
                var ChildResults = from AdefWebserverCategories in CategoriesTable.GetCategoriesTable()
                                   where AdefWebserverCategories.ParentCategoryID == Convert.ToInt32(objListItem.Value)
                                   select AdefWebserverCategories;

                CatagoriesEnumerable children = new CatagoriesEnumerable();

                // 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}", objCategory.CategoryName);
                    objChildListItem.Value = objCategory.CategoryID.ToString();

                    children.Add(new CatagoriesHierarchyData(objChildListItem));
                }

                return children;
            }
            #endregion

            #region GetParent
            public IHierarchyData GetParent()
            {
                CatagoriesEnumerable parentContainer = new CatagoriesEnumerable();

                var ParentResult = (from AdefWebserverCategories in CategoriesTable.GetCategoriesTable()
                                    where AdefWebserverCategories.ParentCategoryID == Convert.ToInt32(objListItem.Value)
                                    select AdefWebserverCategories).FirstOrDefault();

                // 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}", ParentResult.CategoryName);
                objChildListItem.Value = ParentResult.CategoryID.ToString();

                return new CatagoriesHierarchyData(objChildListItem);
            }
            #endregion
        }
        #endregion

    }
    #endregion
}


Buy DotNetNuke Modules from Snowcovered

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