asp.net实现DropDownList,TreeView,ListBox的无限极分类目
网络编程 2021-07-04 22:41www.168986.cn编程入门
这篇文章主要介绍了asp.实现DropDownList,TreeView,ListBox的无限极分类目录树,结合实例形式较为详细的分析了asp.常见控件实现无限极分类目录树的具体实现步骤与相关操作技巧,需要的朋友可以参考下
本文实例讲述了asp.实现DropDownList,TreeView,ListBox的无限极分类目录树。分享给大家供大家参考,具体如下
#region DropDownList无限递归显示层次关系 /// <summary> /// 创建无限分级下拉列表框 /// </summary> /// <param name="ddlst">下拉控件</param> /// <param name="dt">源DataTable</param> /// <param name="text">text字段</param> /// <param name="value">value字段</param> /// <param name="parentid">深度字段 例如parentid</param> 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); } /// <summary> /// 递归绑定子节点 /// </summary> /// <param name="dt">源DataTable</param> /// <param name="items">数组</param> /// <param name="parentRow">当前节点</param> /// <param name="curHeader">前缀</param> /// <param name="text">text字段</param> /// <param name="value">value字段</param> /// <param name="parentid">深度字段 例如parentid</param> 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 无限递归显示层次关系目录树 /// <summary> /// 创建无限分级目录树TreeView /// </summary> /// <param name="treeview">TreeView空间</param> /// <param name="dt">数据源DataTable</param> /// <param name="text">text字段</param> /// <param name="value">value字段</param> /// <param name="parentid">深度字段 例如parentid</param> 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); } } /// <summary> /// 递归绑定子节点 /// </summary> /// <param name="dv">源DataView</param> /// <param name="parentNode">当前节点</param> /// <param name="text">text字段</param> /// <param name="value">value字段</param> /// <param name="parentid">深度字段 例如parentid</param> 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); } } /// <summary> /// 创建无限分级目录树TreeView /// </summary> /// <param name="treeview">TreeView空间</param> /// <param name="dt">数据源DataTable</param> /// <param name="text">text字段</param> /// <param name="value">value字段</param> /// <param name="url">url字段</param> /// <param name="parentid">深度字段 例如parentid</param> 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); } } /// <summary> /// 递归绑定子节点 /// </summary> /// <param name="dv">源DataView</param> /// <param name="parentNode">当前节点</param> /// <param name="text">text字段</param> /// <param name="value">value字段</param> /// <param name="url">url字段</param> /// <param name="parentid">深度字段 例如parentid</param> 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 /// <summary> /// 创建无限分级ListBox /// </summary> /// <param name="ddlst">ListBox控件</param> /// <param name="dt">源DataTable</param> /// <param name="text">text字段</param> /// <param name="value">value字段</param> /// <param name="parentid">深度字段 例如parentid</param> 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); } /// <summary> /// 递归绑定子节点 /// </summary> /// <param name="dt">源DataTable</param> /// <param name="items">数组</param> /// <param name="parentRow">当前节点</param> /// <param name="curHeader">前缀</param> /// <param name="text">text字段</param> /// <param name="value">value字段</param> /// <param name="parentid">深度字段 例如parentid</param> 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.相关内容感兴趣的读者可查看本站专题《》、《》、《》、《》、《》及《》。
希望本文所述对大家asp.程序设计有所帮助。
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程