本文实例讲述了asp.net实现DropDownList,TreeView,ListBox的无限极分类目录树。分享给大家供大家参考,具体如下:
#region DropDownList无限递归显示层次关系 ////// 创建无限分级下拉列表框 /// /// 下拉控件 /// 源DataTable /// text字段 /// value字段 /// 深度字段 例如parentid public static void CreateLevelDropDown(DropDownList ddlst, DataTable dt, string text, string value, string parentid) { ArrayList allItems = new ArrayList(); DataRow[] rows = dt.Select(parentid + "=0"); foreach (DataRow row in rows) CreateLevelDropDownAssistant(dt, ref allItems, row, string.Empty, text, value, parentid); ListItem[] items = new ListItem[allItems.Count]; allItems.CopyTo(items); ddlst.Items.AddRange(items); } ////// 递归绑定子节点 /// /// 源DataTable /// 数组 /// 当前节点 /// 前缀 /// text字段 /// value字段 /// 深度字段 例如parentid private static void CreateLevelDropDownAssistant(DataTable dt, ref ArrayList items, DataRow parentRow, string curHeader, string text, string value, string parentid) { ListItem newItem = new ListItem(curHeader + Until.CutString(parentRow[text].ToString(), 18, true), parentRow[value].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("┗", "┣") + "┣", text, value, parentid); if (rows.Length > 0) CreateLevelDropDownAssistant(dt, ref items, rows[rows.Length - 1], curHeader.Replace("┣", "┃").Replace("┗", "┣") + "┗", text, value, parentid); } #endregion #region TreeView 无限递归显示层次关系目录树 ////// 创建无限分级目录树TreeView /// /// TreeView空间 /// 数据源DataTable /// text字段 /// value字段 /// 深度字段 例如parentid public static void CreateLevelTreeView(TreeView treeview, DataTable dt, string text, string value, string parentid) { DataView dv = dt.DefaultView; dv.RowFilter = parentid + "=0"; foreach (DataRowView drv in dv) { TreeNode node = new TreeNode(); node.Text = drv[text].ToString(); node.Value = drv[value].ToString(); node.Expanded = false; treeview.Nodes.Add(node); CreatTreeViewChildNode(dv, node, text, value, parentid); } } ////// 递归绑定子节点 /// /// 源DataView /// 当前节点 /// text字段 /// value字段 /// 深度字段 例如parentid private static void CreatTreeViewChildNode(DataView dv, TreeNode parentNode, string text, string value, string parentid) { dv.RowFilter = parentid + "=" + parentNode.Value; foreach (DataRowView row in dv) { TreeNode replyNode = new TreeNode(); replyNode.Text = row[text].ToString(); replyNode.Value = row[value].ToString(); replyNode.Expanded = false; parentNode.ChildNodes.Add(replyNode); CreatTreeViewChildNode(dv, replyNode, text, value, parentid); } } ////// 创建无限分级目录树TreeView /// /// TreeView空间 /// 数据源DataTable /// text字段 /// value字段 /// url字段 /// 深度字段 例如parentid public static void CreateLevelTreeView(TreeView treeview, DataTable dt, string text, string value, string url, string parentid) { DataView dv = dt.DefaultView; dv.RowFilter = parentid + "=0"; foreach (DataRowView drv in dv) { TreeNode node = new TreeNode(); node.Text = drv[text].ToString(); node.Value = drv[value].ToString(); node.NavigateUrl = drv[url].ToString(); node.Expanded = false; treeview.Nodes.Add(node); CreatTreeViewChildNode(dv, node, text, value, url, parentid); } } ////// 递归绑定子节点 /// /// 源DataView /// 当前节点 /// text字段 /// value字段 /// url字段 /// 深度字段 例如parentid private static void CreatTreeViewChildNode(DataView dv, TreeNode parentNode, string text, string value, string url, string parentid) { dv.RowFilter = parentid + "=" + parentNode.Value; foreach (DataRowView row in dv) { TreeNode replyNode = new TreeNode(); replyNode.Text = row[text].ToString(); replyNode.Value = row[value].ToString(); replyNode.NavigateUrl = row[url].ToString(); replyNode.Expanded = false; parentNode.ChildNodes.Add(replyNode); CreatTreeViewChildNode(dv, replyNode, text, value, url, parentid); } } #endregion #region 创建无限分级ListBox ////// 创建无限分级ListBox /// /// ListBox控件 /// 源DataTable /// text字段 /// value字段 /// 深度字段 例如parentid public static void CreateLevelListBox(ListBox ddlst, DataTable dt, string text, string value, string parentid) { ArrayList allItems = new ArrayList(); DataRow[] rows = dt.Select(parentid + "=0"); foreach (DataRow row in rows) CreateLevelListBoxAssistant(dt, ref allItems, row, string.Empty, text, value, parentid); ListItem[] items = new ListItem[allItems.Count]; allItems.CopyTo(items); ddlst.Items.AddRange(items); } ////// 递归绑定子节点 /// /// 源DataTable /// 数组 /// 当前节点 /// 前缀 /// text字段 /// value字段 /// 深度字段 例如parentid private static void CreateLevelListBoxAssistant(DataTable dt, ref ArrayList items, DataRow parentRow, string curHeader, string text, string value, string parentid) { ListItem newItem = new ListItem(curHeader + Until.CutString(parentRow[text].ToString(), 18, true), parentRow[value].ToString()); items.Add(newItem); DataRow[] rows = dt.Select(parentid + "=" + newItem.Value); for (int i = 0; i < rows.Length - 1; i++) CreateLevelListBoxAssistant(dt, ref items, rows[i], curHeader.Replace("┣", "┃").Replace("┗", "┣") + "┣", text, value, parentid); if (rows.Length > 0) CreateLevelListBoxAssistant(dt, ref items, rows[rows.Length - 1], curHeader.Replace("┣", "┃").Replace("┗", "┣") + "┗", text, value, parentid); } #endregion
更多关于asp.net相关内容感兴趣的读者可查看本站专题:《asp.net操作json技巧总结》、《asp.net字符串操作技巧汇总》、《asp.net操作XML技巧总结》、《asp.net文件操作技巧汇总》、《asp.net ajax技巧总结专题》及《asp.net缓存操作技巧总结》。
希望本文所述对大家asp.net程序设计有所帮助。