这篇文章主要为大家详细介绍了C#实现无限级联下拉列表框的相关资料,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了无限级联下拉列表框的的实现方法,具体内容如下
可能有一个树型结构的表,它可能有ID,Name,ParentID,Level等字段,下面要实现的就是从一级节点开始,一级一级的列出来,并以
下拉列表框的形式体现出来,就像是N级联动。
效果图:

两个问题:
1、建立操作时的联动,它不需要进行自动绑定
2、编辑操作时的联运,它需要根据子节点,逐级自己绑定到父节点,直到根
实现:
JS代码
<script type="text/javascript">
function areaOnSelect(obj) {
var res = '';
$.ajax({ url: '@Url.Action("GetSubTree")',
type: 'GET',
data: { parentId: obj.value },
success: function (msg) {
$(obj).nextAll().remove();
res = "<select name='Sub' onchange='areaOnSelect(this)'>";
res += "<option value=''>请选择</option>";
$.each(msg, function (i, item) {
res += "<option value='" + item["ID"] + "'>" + item["Name"] + "</option>";
});
res += "</select>";
if ($(res).find("option").size() > 1)
$(obj).after(res);
}
});
}
</script>
C#代码:
#region 树型结构相关
/// <summary>
/// 递归找老祖宗
/// </summary>
/// <param name="father"></param>
void GetFather(SubItem father)
{
if (father != null)
{
father.Parent = _subList.FirstOrDefault(i => i.ID == father.ParentID);
GetFather(father.Parent);
}
}
/// <summary>
/// 弟妹找子孙
/// </summary>
/// <param name="father">父对象</param>
void getSons(SubItem father)
{
if (father != null)
{
father.Sons = _subList.Where(item =>
item.ParentID.Equals(father.ID)).ToList();
father.Sons.ForEach(item =>
{
item.Parent = father;
getSons(item);
});
}
}
#endregion










