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

树形结构 DropDownList

时间:2014-09-23 19:55:05      阅读:304      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   ar   for   div   sp   on   

前台

   <asp:DropDownList ID="ddlOutType" runat="server" />

后台代码:


先绑定第一级

                string SQL = "SELECT CategoryID,CategoryName,ParentID FROM Category"    
            DataTable datatable = SqlHelper.ExecuteDataTable(SQL);
                DataRow[] row = datatable.Select("ParentID=‘0‘");
                //添加根目录
                for (int i = 0; i < row.Length; i++)
                {
                    ListItem li = new ListItem(row[i]["CategoryName"].ToString(), row[i]["CategoryID"].ToString());
                    ddlOutType.Items.Add(li);
                    addOtherDll("", Convert.ToInt32(row[i]["CategoryID"]), datatable, 1, ddlOutType);
                }


递归查找下级

      /// <param name="Pading">空格</param>
        /// <param name="DirId">父路径ID</param>
        /// <param name="datatable">返回的datatable</param>
        /// <param name="deep">树形的深度</param>
        private void addOtherDll(string Pading, int DirId, DataTable datatable, int deep,DropDownList ddl)
        {
            DataRow[] rowlist = datatable.Select("ParentID=‘" + DirId + "");
            foreach (DataRow row in rowlist)
            {
                string strPading = "";
                for (int j = 0; j < deep; j++)
                {
                    strPading += " "; //用全角的空格
                }
                //添加节点
                ListItem li = new ListItem(strPading + "|--" + row["CategoryName"].ToString(),row["CategoryID"].ToString());
                ddl.Items.Add(li);
                //递归调用addOtherDll函数,在函数中把deep加1
                addOtherDll(strPading, Convert.ToInt32(row["CategoryID"]), datatable, deep + 1, ddl);
            }
        }

 

树形结构 DropDownList

标签:style   blog   color   os   ar   for   div   sp   on   

原文地址:http://www.cnblogs.com/linyijia/p/3988849.html

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