当前位置: 七九推 > IT编程>开发语言>.net > ASP.NET C#生成下拉列表树实现代码

ASP.NET C#生成下拉列表树实现代码

2023年01月25日 .net 我要评论
效果图: 代码: 复制代码 代码如下: using system.data; using system.web.ui.webcontrols; /// <summary> /// 根据da
效果图:

代码:
复制代码 代码如下:

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);
(0)
打赏 微信扫一扫 微信扫一扫

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2023  七九推 保留所有权利. 粤ICP备17035492号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com