主页 > 知识库 > asp.net实现DropDownList,TreeView,ListBox的无限极分类目录树

asp.net实现DropDownList,TreeView,ListBox的无限极分类目录树

热门标签:硅谷的囚徒呼叫中心 呼叫中心 地方门户网站 电子围栏 解决方案 苹果 智能手机 服务器配置

本文实例讲述了asp.net实现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.net相关内容感兴趣的读者可查看本站专题:《asp.net操作json技巧总结》、《asp.net字符串操作技巧汇总》、《asp.net操作XML技巧总结》、《asp.net文件操作技巧汇总》、《asp.net ajax技巧总结专题》及《asp.net缓存操作技巧总结》。

希望本文所述对大家asp.net程序设计有所帮助。

您可能感兴趣的文章:
  • Jquery.TreeView结合ASP.Net和数据库生成菜单导航条
  • ASP.NET生成树形显示的GridView实现思路
  • 常用的在数据库中建立无限级树形菜单的asp.net代码
  • asp.net 获取指定文件夹下所有子目录及文件(树形)
  • asp.net实现递归方法取出菜单并显示在DropDownList中(分栏形式)
  • ASP.NET递归法求阶乘解决思路
  • 递归输出ASP.NET页面所有控件的类型和ID的代码
  • asp.net TreeView与XML三步生成列表树
  • Asp.net treeview实现无限级树实现代码
  • asp.net TreeView递归循环子节点生成树形菜单实例

标签:喀什 佳木斯 泰安 房产 海口 德宏 玉林 吕梁

巨人网络通讯声明:本文标题《asp.net实现DropDownList,TreeView,ListBox的无限极分类目录树》,本文关键词  ;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。
  • 相关文章
  • 收缩
    • 微信客服
    • 微信二维码
    • 电话咨询

    • 400-1100-266