ASP.NET C#生成下拉列表树实现代码
程序员文章站
2024-03-05 10:48:48
效果图: 代码: 复制代码 代码如下: using system.data; using system.web.ui.webcontrols; ///
效果图:
![ASP.NET C#生成下拉列表树实现代码 ASP.NET C#生成下拉列表树实现代码](/default/index/img?u=aHR0cDovL2ltYWdlcy4xMHFpYW53YW4uY29tLzEwcWlhbndhbi8yMDE3MTIxMi9iXzFfMjAxNzEyMTIxOTI1NTExMDcxLmpwZw==)
代码:
using system.data;
using system.web.ui.webcontrols;
/// <summary>
/// 根据datatable生成下拉列表树
/// </summary>
public class dropdownlisthelp
{
private string gridline;
private datatable dt;
public dropdownlisthelp()
{
//
//todo: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 根据datatable生成树形下拉菜单
/// </summary>
/// <param name="datatable"></param>
/// <param name="parentkeyfield">上级节点关键字段</param>
/// <param name="parentkey">上级节点值</param>
/// <param name="keyfield">本节点关键字段</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>
/// 递归生成树节点
/// </summary>
/// <param name="parentkeyfield">上级节点关键字段</param>
/// <param name="parentkey">上级节点值</param>
/// <param name="keyfield">本节点关键字段</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">上级节点关键字段</param>
/// <param name="parentkey">上级节点值</param>
/// <param name="keyfield">本节点关键字段</param>
/// <param name="nodekey">本节点值</param>
/// <param name="sortstring">排序字符串</param>
/// <returns></returns>
private void gettreeline(string parentkeyfield, string parentkey, string keyfield, string nodekey, string sortstring)
{
//选择父层节点
dataview dv = new dataview(dt, keyfield + "='" + parentkey + "'", sortstring, dataviewrowstate.currentrows);
if (dv.count > 0)
{
//选择父节点同级节点
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();
}
}
调用:
dropdownlisthelp ddlhelper = new dropdownlisthelp();
ddlhelper.createdropdowntree(dt, "parentid", "1", "id","text", "text asc", dropdownlist1);
代码:
复制代码 代码如下:
using system.data;
using system.web.ui.webcontrols;
/// <summary>
/// 根据datatable生成下拉列表树
/// </summary>
public class dropdownlisthelp
{
private string gridline;
private datatable dt;
public dropdownlisthelp()
{
//
//todo: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 根据datatable生成树形下拉菜单
/// </summary>
/// <param name="datatable"></param>
/// <param name="parentkeyfield">上级节点关键字段</param>
/// <param name="parentkey">上级节点值</param>
/// <param name="keyfield">本节点关键字段</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>
/// 递归生成树节点
/// </summary>
/// <param name="parentkeyfield">上级节点关键字段</param>
/// <param name="parentkey">上级节点值</param>
/// <param name="keyfield">本节点关键字段</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">上级节点关键字段</param>
/// <param name="parentkey">上级节点值</param>
/// <param name="keyfield">本节点关键字段</param>
/// <param name="nodekey">本节点值</param>
/// <param name="sortstring">排序字符串</param>
/// <returns></returns>
private void gettreeline(string parentkeyfield, string parentkey, string keyfield, string nodekey, string sortstring)
{
//选择父层节点
dataview dv = new dataview(dt, keyfield + "='" + parentkey + "'", sortstring, dataviewrowstate.currentrows);
if (dv.count > 0)
{
//选择父节点同级节点
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();
}
}
调用:
复制代码 代码如下:
dropdownlisthelp ddlhelper = new dropdownlisthelp();
ddlhelper.createdropdowntree(dt, "parentid", "1", "id","text", "text asc", dropdownlist1);