zoukankan      html  css  js  c++  java
  • DroDownList控件多级下拉菜单

    后台代码:

        /// <summary>
            /// 绑定下拉菜单
            /// </summary>
            private void BindDropList()
            {
                DataTable dt = bacManage.GetAllArticleCategory();
                CreateLevelDropDown(drpCategoryId, dt);
            }
    
            /// <summary>
            /// 创建分级下拉框
            /// </summary>
            /// <param name="ddlst"></param>
            /// <param name="dt"></param>
            private void CreateLevelDropDown(DropDownList ddlst, DataTable dt)
            {
                System.Collections.ArrayList allItems = new ArrayList();
                DataRow[] rows = dt.Select("[parent]=" + 0);
                foreach (DataRow row in rows)
                    CreateLevelDropDownAssistant(dt, ref   allItems, row, string.Empty);
    
                ListItem[] items = new ListItem[allItems.Count];
                allItems.CopyTo(items);
                ddlst.Items.AddRange(items);
            }
    
            /// <summary>
            /// 设置下拉列表分级格式
            /// </summary>
            /// <param name="dt"></param>
            /// <param name="items"></param>
            /// <param name="parentRow"></param>
            /// <param name="curHeader"></param>
            private void CreateLevelDropDownAssistant(DataTable dt, ref   ArrayList items, DataRow parentRow, string curHeader)
            {
                ListItem newItem = new ListItem(curHeader + parentRow["name"].ToString(), parentRow["category_id"].ToString());
                items.Add(newItem);
                parentRow.Delete();
    
                DataRow[] rows = dt.Select("[parent]='" + 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("", "") + "");
            }

        /// <summary>
            /// 绑定下拉菜单
            /// </summary>
            private void BindDropList()
            {
                DataTable dt = bacManage.GetAllArticleCategory();
                CreateLevelDropDown(drpCategoryId, dt);
            }

            /// <summary>
            /// 创建分级下拉框
            /// </summary>
            /// <param name="ddlst"></param>
            /// <param name="dt"></param>
            private void CreateLevelDropDown(DropDownList ddlst, DataTable dt)
            {
                System.Collections.ArrayList allItems = new ArrayList();
                DataRow[] rows = dt.Select("[parent]=" + 0);
                foreach (DataRow row in rows)
                    CreateLevelDropDownAssistant(dt, ref   allItems, row, string.Empty);

                ListItem[] items = new ListItem[allItems.Count];
                allItems.CopyTo(items);
                ddlst.Items.AddRange(items);
            }

            /// <summary>
            /// 设置下拉列表分级格式
            /// </summary>
            /// <param name="dt"></param>
            /// <param name="items"></param>
            /// <param name="parentRow"></param>
            /// <param name="curHeader"></param>
            private void CreateLevelDropDownAssistant(DataTable dt, ref   ArrayList items, DataRow parentRow, string curHeader)
            {
                ListItem newItem = new ListItem(curHeader + parentRow["name"].ToString(), parentRow["category_id"].ToString());
                items.Add(newItem);
                parentRow.Delete();

                DataRow[] rows = dt.Select("[parent]='" + 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("┗", "┣") + "┗");
            }

  • 相关阅读:
    辅助随笔:因知识点不足暂时错过的题目
    NOIP2019翻车前写(and 抄)过的代码
    NOIP2019翻车前计划以及日记
    Luogu P3706 [SDOI2017]硬币游戏
    Luogu P5296 [北京省选集训2019]生成树计数
    Luogu P3307 [SDOI2013]项链
    Gaussian整数
    Problem. S
    LOJ6696 复读机 加强版
    数据库约束
  • 原文地址:https://www.cnblogs.com/acoll/p/3834193.html
Copyright © 2011-2022 走看看