程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> asp.net實現DropDownList,TreeView,ListBox的無限極分類目錄樹

asp.net實現DropDownList,TreeView,ListBox的無限極分類目錄樹

編輯:ASP.NET基礎

本文實例講述了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程序設計有所幫助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved