asp.net實(shí)現(xiàn)DropDownList,TreeView,ListBox的無(wú)限極分類目錄樹
來(lái)源:易賢網(wǎng) 閱讀:1096 次 日期:2016-08-08 14:01:51
溫馨提示:易賢網(wǎng)小編為您整理了“asp.net實(shí)現(xiàn)DropDownList,TreeView,ListBox的無(wú)限極分類目錄樹”,方便廣大網(wǎng)友查閱!

本文實(shí)例講述了asp.net實(shí)現(xiàn)DropDownList,TreeView,ListBox的無(wú)限極分類目錄樹。分享給大家供大家參考,具體如下:

#region DropDownList無(wú)限遞歸顯示層次關(guān)系

/// <summary>

/// 創(chuàng)建無(wú)限分級(jí)下拉列表框

/// </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>

/// 遞歸綁定子節(jié)點(diǎn)

/// </summary>

/// <param name="dt">源DataTable</param>

/// <param name="items">數(shù)組</param>

/// <param name="parentRow">當(dāng)前節(jié)點(diǎn)</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 無(wú)限遞歸顯示層次關(guān)系目錄樹

/// <summary>

/// 創(chuàng)建無(wú)限分級(jí)目錄樹TreeView

/// </summary>

/// <param name="treeview">TreeView空間</param>

/// <param name="dt">數(shù)據(jù)源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>

/// 遞歸綁定子節(jié)點(diǎn)

/// </summary>

/// <param name="dv">源DataView</param>

/// <param name="parentNode">當(dāng)前節(jié)點(diǎn)</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>

/// 創(chuàng)建無(wú)限分級(jí)目錄樹TreeView

/// </summary>

/// <param name="treeview">TreeView空間</param>

/// <param name="dt">數(shù)據(jù)源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>

/// 遞歸綁定子節(jié)點(diǎn)

/// </summary>

/// <param name="dv">源DataView</param>

/// <param name="parentNode">當(dāng)前節(jié)點(diǎn)</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 創(chuàng)建無(wú)限分級(jí)ListBox

/// <summary>

/// 創(chuàng)建無(wú)限分級(jí)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>

/// 遞歸綁定子節(jié)點(diǎn)

/// </summary>

/// <param name="dt">源DataTable</param>

/// <param name="items">數(shù)組</param>

/// <param name="parentRow">當(dāng)前節(jié)點(diǎn)</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

希望本文所述對(duì)大家asp.net程序設(shè)計(jì)有所幫助。

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)