欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-11 17:26:28
本文实例讲述了asp.net实现dropdownlist,treeview,listbox的无限极分类目录树。分享给大家供大家参考,具体如下: #region d...

本文实例讲述了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程序设计有所帮助。