码迷,mamicode.com
首页 > 其他好文 > 详细

DroDownList控件多级下拉菜单

时间:2014-07-11 10:54:41      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   art   cti   

后台代码:

    /// <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控件多级下拉菜单

标签:style   blog   color   os   art   cti   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!