DropDownList 下拉无限极分类代码

       #region 无限极分类;
        /// <summary>
        /// 绑定DropDownList;
        /// </summary>
        protected void BindDropdownList()
        {
            DataTable dt = new DataTable();
            dt = bllClass.GetAllList().Tables[0];
            CreateLevelDropDown(ddlType, dt);
            //ddlType.Items.Insert(ddlType.Items.Count, new ListItem("——此类为根类——", "0"));
            ListItem tempItemNull = ddlType.Items.FindByValue("0");
        }
        /// <summary>
        ///实现无限极分类;
        /// </summary>
        /// <param name="ddlst"></param>
        /// <param name="dt"></param>
        protected void CreateLevelDropDown(DropDownList ddlst, DataTable dt)
        {
            ArrayList allItems = new ArrayList();
            DataRow[] rows = dt.Select("ParentId=" + 0);
            foreach (DataRow row in rows)
            {
                CreateLevelDropDownAssistant(dt, ref allItems, row, string.Empty);
                ListItem[] items = new ListItem[allItems.Count];
                allItems.CopyTo(items);
                ddlst.Items.Clear();
                ddlst.Items.AddRange(items);
            }
        }
        private void CreateLevelDropDownAssistant(DataTable dt, ref   ArrayList items, DataRow parentRow, string curHeader)
        {
            ListItem newItem = new ListItem(curHeader + parentRow["ClassName"].ToString(), parentRow["ClassId"].ToString());
            items.Add(newItem);
            DataRow[] rows = dt.Select("ParentId=" + newItem.Value);
            for (int i = 0; i < rows.Length - 1; i++)
                CreateLevelDropDownAssistant(dt, ref   items, rows[i], curHeader.Replace("┣", "┃").Replace("┗", "┣") + "┣");

            if (rows.Length > 0)
                CreateLevelDropDownAssistant(dt, ref   items, rows[rows.Length - 1], curHeader.Replace("┣", "┃").Replace("┗", "┣") + "┗");
        }
        #endregion

你可能感兴趣的:(list)