首页 > 其他 > 详细

DroDownList控件多级下拉菜单

时间:2014-07-11 10:53:43      阅读:446      评论:0      收藏:0      [点我收藏+]

后台代码:

    /// <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("┗", "┣") + "┗");
        }

DroDownList控件多级下拉菜单,布布扣,bubuko.com

DroDownList控件多级下拉菜单

原文:http://www.cnblogs.com/acoll/p/3834193.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!