主頁 > 知識庫 > ASP.NET C#生成下拉列表樹實現(xiàn)代碼

ASP.NET C#生成下拉列表樹實現(xiàn)代碼

熱門標簽:南陽外呼系統(tǒng)定制化 合肥電銷外呼系統(tǒng)哪家公司做的好 百度ai地圖標注 蘋果手機凱立德地圖標注 申請400電話手續(xù) 同安公安400電話怎么申請流程 玉林市機器人外呼系統(tǒng)哪家好 預測式外呼系統(tǒng)使用說明 電話機器人軟件銷售工作
效果圖:

代碼:
復制代碼 代碼如下:

using System.Data;
using System.Web.UI.WebControls;
/// summary>
/// 根據(jù)DataTable生成下拉列表樹
/// /summary>
public class DropDownListHelp
{
private string gridline;
private DataTable dt;
public DropDownListHelp()
{
//
//TODO: 在此處添加構(gòu)造函數(shù)邏輯
//
}
/// summary>
/// 根據(jù)Datatable生成樹形下拉菜單
/// /summary>
/// param name="datatable">/param>
/// param name="parentKeyField">上級節(jié)點關鍵字段/param>
/// param name="parentKey">上級節(jié)點值/param>
/// param name="keyField">本節(jié)點關鍵字段/param>
/// param name="sortString">排序字符串/param>
/// param name="ddl">DownList/param>
public void createDropDownTree(DataTable datatable, string parentKeyField, string parentKey, string keyField, string textField, string sortString, DropDownList ddl)
{
dt = datatable;
ddl.Items.Add(new ListItem("", ""));
addChildItems(parentKeyField, parentKey, keyField, textField, sortString, ddl);
}
/// summary>
/// 遞歸生成樹節(jié)點
/// /summary>
/// param name="parentKeyField">上級節(jié)點關鍵字段/param>
/// param name="parentKey">上級節(jié)點值/param>
/// param name="keyField">本節(jié)點關鍵字段/param>
/// param name="sortString">排序字符串/param>
/// param name="ddl">DownList控件/param>
/// returns>/returns>
private void addChildItems(string parentKeyField, string parentKey, string keyField, string textField, string sortString, DropDownList ddl)
{
DataView dv = new DataView(dt, parentKeyField + "='" + parentKey + "'", sortString, DataViewRowState.CurrentRows);
int a = dv.Count;
if (dv.Count == 0)
{
return;
}
for (int i = 0; i a; i++)
{
gridline = "";
dv.RowFilter = parentKeyField + "='" + parentKey + "'";
dv.Sort = sortString;
getTreeLine(parentKeyField, dv[i][parentKeyField].ToString(), keyField, dv[i][keyField].ToString(), sortString);
dv.RowFilter = parentKeyField + "='" + parentKey + "'";
dv.Sort = sortString;
ddl.Items.Add(new ListItem(gridline + (i == a - 1 ? "┗" : "┣") + dv[i][textField].ToString(), dv[i][keyField].ToString()));
addChildItems(parentKeyField, dv[i][keyField].ToString(), keyField, textField, sortString, ddl);
}
dv.Dispose();
}
/// summary>
/// 回溯生成樹的連接線
/// /summary>
/// param name="parentKeyField">上級節(jié)點關鍵字段/param>
/// param name="parentKey">上級節(jié)點值/param>
/// param name="keyField">本節(jié)點關鍵字段/param>
/// param name="nodeKey">本節(jié)點值/param>
/// param name="sortString">排序字符串/param>
/// returns>/returns>
private void getTreeLine(string parentKeyField, string parentKey, string keyField, string nodeKey, string sortString)
{
//選擇父層節(jié)點
DataView dv = new DataView(dt, keyField + "='" + parentKey + "'", sortString, DataViewRowState.CurrentRows);
if (dv.Count > 0)
{
//選擇父節(jié)點同級節(jié)點
dv.RowFilter = parentKeyField + "='" + dv[0][parentKeyField].ToString() + "'";
dv.Sort = sortString;
for (int j = 0; j dv.Count; j++)
{
if (dv[j][keyField].ToString() == parentKey)
{
if (j == dv.Count - 1)
{
gridline = " " + gridline;
}
else
{
gridline = "┃" + gridline;
}
}
}
getTreeLine(parentKeyField, dv[0][parentKeyField].ToString(), keyField, dv[0][keyField].ToString(), sortString);
}
dv.Dispose();
}
}

調(diào)用:
復制代碼 代碼如下:

DropDownListHelp ddlHelper = new DropDownListHelp();
ddlHelper.createDropDownTree(dt, "parentID", "1", "ID","Text", "Text asc", DropDownList1);
您可能感興趣的文章:
  • C#之Expression表達式樹實例
  • 關于c#二叉樹的實現(xiàn)
  • C# 遞歸查找樹狀目錄實現(xiàn)方法
  • 一個很簡單的jquery+xml+ajax的無刷新樹結(jié)構(gòu)(無css,后臺是c#)
  • C#實現(xiàn)獲取系統(tǒng)目錄并以Tree樹叉顯示的方法
  • C# TreeView無限目錄樹實現(xiàn)方法
  • C#使用前序遍歷、中序遍歷和后序遍歷打印二叉樹的方法
  • C#求解哈夫曼樹,實例代碼
  • c#反射表達式樹模糊搜索示例
  • C#通過KD樹進行距離最近點的查找

標簽:揚州 嘉興 南昌 臺州 海南 南京 淄博 南京

巨人網(wǎng)絡通訊聲明:本文標題《ASP.NET C#生成下拉列表樹實現(xiàn)代碼》,本文關鍵詞  ASP.NET,生成,下拉,列表,樹,;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《ASP.NET C#生成下拉列表樹實現(xiàn)代碼》相關的同類信息!
  • 本頁收集關于ASP.NET C#生成下拉列表樹實現(xiàn)代碼的相關信息資訊供網(wǎng)民參考!
  • 推薦文章