博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
带线的无限级下拉树列表
阅读量:5737 次
发布时间:2019-06-18

本文共 5239 字,大约阅读时间需要 17 分钟。

好多年没写文章了

这里就分享点自己原创的一点破代码,效果如图下:
r_%7BDB83491B-1991-48D4-9A9B-69704E20B45
本人的提供的代码如下:

None.gif
using System;
None.gif
using System.Collections.Generic;
None.gif
using System.Text;
None.gif
using System.Web.UI.WebControls;
None.gif
None.gif
namespace Interface.Common
ExpandedBlockStart.gif
ContractedBlock.gif
dot.gif {
InBlock.gif    
public 
interface IDropDownTree : IDisposable
ExpandedSubBlockStart.gif
ContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gif
ContractedSubBlock.gif        
/**/
///
 
<summary>
InBlock.gif        
///
 返回Dictionary里分别对应ID,文本,如果没有子节点返回null
InBlock.gif        
///
 
</summary>
InBlock.gif        
///
 
<param name="parentID">
父节点ID
</param>
ExpandedSubBlockEnd.gif        
///
 
<returns></returns>
InBlock.gif        Dictionary<
string
string> GetChildCategory(
string parentID);
ExpandedSubBlockStart.gif
ContractedSubBlock.gif        
/**/
///
 
<summary>
InBlock.gif        
///
 代码里写return new Interface.Common.DropDownTree(this);
ExpandedSubBlockEnd.gif        
///
 
</summary>
InBlock.gif        DropDownTree DropDownTree
ExpandedSubBlockStart.gif
ContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get;
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockEnd.gif    }
InBlock.gif    
public 
sealed 
class DropDownTree
ExpandedSubBlockStart.gif
ContractedSubBlock.gif    
dot.gif{
InBlock.gif        IDropDownTree _DropDownTree;
InBlock.gif        
public DropDownTree(IDropDownTree dropDownTree)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif        
dot.gif{
InBlock.gif            _DropDownTree = dropDownTree;
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockStart.gif
ContractedSubBlock.gif        
/**/
///
 
<summary>
InBlock.gif        
///
 用于树的前缀
InBlock.gif        
///
 
</summary>
InBlock.gif        
///
 
<param name="IsLast">
是否是同级节点中的最后一个
</param>
InBlock.gif        
///
 
<param name="HasChild">
本节点是否拥有子节点
</param>
InBlock.gif        
///
 
<param name="ParentString">
父节点前缀符号
</param>
ExpandedSubBlockEnd.gif        
///
 
<returns>
本节点的前缀
</returns>
InBlock.gif        
private 
string GetPreFix(
bool isLast, 
bool hasChild, 
string parentString)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string result = 
string.Empty;
InBlock.gif            
if (!
string.IsNullOrEmpty(parentString))
ExpandedSubBlockStart.gif
ContractedSubBlock.gif            
dot.gif{
InBlock.gif                parentString = parentString.Remove(parentString.Length - 
1).Replace(
"
"
"
").Replace(
"
"
"
 
");
InBlock.gif                result += parentString;
ExpandedSubBlockEnd.gif            }
InBlock.gif            
if (isLast)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif            
dot.gif{
InBlock.gif                result += 
"
";
ExpandedSubBlockEnd.gif            }
InBlock.gif            
else
ExpandedSubBlockStart.gif
ContractedSubBlock.gif            
dot.gif{
InBlock.gif                result += 
"
";
ExpandedSubBlockEnd.gif            }
InBlock.gif            
if (hasChild)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif            
dot.gif{
InBlock.gif                result += 
"
";
ExpandedSubBlockEnd.gif            }
InBlock.gif            
else
ExpandedSubBlockStart.gif
ContractedSubBlock.gif            
dot.gif{
InBlock.gif                result += 
"
";
ExpandedSubBlockEnd.gif            }
InBlock.gif            
return result;
ExpandedSubBlockEnd.gif        }
ContractedSubBlock.gif
ExpandedSubBlockStart.gif        
绑定下拉菜单
#region 绑定下拉菜单
InBlock.gif
ExpandedSubBlockStart.gif
ContractedSubBlock.gif        
/**/
///
 
<summary>
InBlock.gif        
///
 绑定连动级的下拉菜单
InBlock.gif        
///
 
</summary>
InBlock.gif        
///
 
<param name="ddlGoodsType">
传进一个被绑定的DropDownList
</param>
InBlock.gif        
///
 
<param name="removeID">
被排除绑定的节点ID
</param>
ExpandedSubBlockEnd.gif        
///
 
<param name="AutoDispose">
是否自动释放
</param>
InBlock.gif        
public 
void BindToDropDownList(DropDownList ddlGoodsType, 
string removeID,
string parentID, 
bool autoDispose)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (ddlGoodsType != 
null)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif            
dot.gif{
InBlock.gif                ListItem listItem = 
null;
InBlock.gif                
string currentID = parentID;
//
根节点/父ID
InBlock.gif
                
string currentSign = 
string.Empty;
//
当前节点符号;
InBlock.gif
                
string parrentSign = 
string.Empty; 
//
父节点符号;
InBlock.gif
                
bool HasChild = 
true;
//
是否有子
InBlock.gif
                Queue<
string> parentKeyList = 
new Queue<
string>();
//
存 有子节点的 节点ID
InBlock.gif
                Queue<
string> parentSignList = 
new Queue<
string>();
//
对应节点ID的前缀符号
InBlock.gif
                
int itemIndexOf = 
0;
//
父节点所在的位置 
InBlock.gif
                
while (HasChild)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
int lastOneCount = 
1;
//
用于计算在同级别中是否最后一个
InBlock.gif
                    Dictionary<
string
string> childList = _DropDownTree.GetChildCategory(currentID);
//
 得到子节点列表
InBlock.gif
                    
if (childList != 
null && childList.Count > 
0)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
if (!
string.IsNullOrEmpty(removeID) && childList.ContainsKey(removeID))
ExpandedSubBlockStart.gif
ContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            childList.Remove(removeID);
ExpandedSubBlockEnd.gif                        }
InBlock.gif                        
foreach (KeyValuePair<
string
string> entry 
in childList)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
if (_DropDownTree.GetChildCategory(entry.Key) != 
null)
//
存在子
ExpandedSubBlockStart.gifContractedSubBlock.gif
                            
dot.gif{
InBlock.gif                                currentSign = GetPreFix(lastOneCount == childList.Count, 
true, parrentSign);
InBlock.gif                                listItem = 
new ListItem(currentSign + entry.Value, entry.Key);
InBlock.gif
InBlock.gif                                parentKeyList.Enqueue(entry.Key);
//
当前的节点ID
InBlock.gif
                                parentSignList.Enqueue(currentSign);
//
当前的节点符号
ExpandedSubBlockEnd.gif
                            }
InBlock.gif                            
else
//
不存在子
ExpandedSubBlockStart.gifContractedSubBlock.gif
                            
dot.gif{
InBlock.gif                                currentSign = GetPreFix(lastOneCount == childList.Count, 
false, parrentSign);
InBlock.gif                                listItem = 
new ListItem(currentSign + entry.Value, entry.Key);
ExpandedSubBlockEnd.gif                            }
InBlock.gif                            
if (ddlGoodsType.Items.Count != 
0)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                itemIndexOf = 
string.IsNullOrEmpty(currentID) ? itemIndexOf + 
1 : ddlGoodsType.Items.IndexOf(ddlGoodsType.Items.FindByValue(currentID)) + lastOneCount;
ExpandedSubBlockEnd.gif                            }
InBlock.gif                            ddlGoodsType.Items.Insert(itemIndexOf, listItem);
//
添加子节点
InBlock.gif
                            lastOneCount++;
ExpandedSubBlockEnd.gif                        }
InBlock.gif                        
if (parentKeyList.Count > 
0)
//
存在子节点时
ExpandedSubBlockStart.gifContractedSubBlock.gif
                        
dot.gif{
InBlock.gif                            currentID = parentKeyList.Dequeue();
InBlock.gif                            parrentSign = parentSignList.Dequeue();
ExpandedSubBlockEnd.gif                        }
InBlock.gif                        
else
ExpandedSubBlockStart.gif
ContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            HasChild = 
false;
ExpandedSubBlockEnd.gif                        }
ExpandedSubBlockEnd.gif                    }
InBlock.gif                    
else
ExpandedSubBlockStart.gif
ContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
break;
ExpandedSubBlockEnd.gif                    }
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif                }
InBlock.gif                
if (autoDispose)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif                
dot.gif{
InBlock.gif                    _DropDownTree.Dispose();
ExpandedSubBlockEnd.gif                }
InBlock.gif
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockStart.gif
ContractedSubBlock.gif        
/**/
///
 
<summary>
InBlock.gif        
///
 绑定连动级的下拉菜单
InBlock.gif        
///
 
</summary>
ExpandedSubBlockEnd.gif        
///
 
<param name="ddlGoodsType">
传进一个被绑定的DropDownList
</param>
InBlock.gif        
public 
void BindToDropDownList(DropDownList ddlGoodsType)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif        
dot.gif{
InBlock.gif            BindToDropDownList(ddlGoodsType, 
string.Empty,
null
true);
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockStart.gif
ContractedSubBlock.gif        
/**/
///
 
<summary>
InBlock.gif        
///
 绑定连动级的下拉菜单
InBlock.gif        
///
 
</summary>
InBlock.gif        
///
 
<param name="ddlGoodsType">
传进一个被绑定的DropDownList
</param>
ExpandedSubBlockEnd.gif        
///
 
<param name="removeID">
被排除的ID
</param>
InBlock.gif        
public 
void BindToDropDownList(DropDownList ddlGoodsType, 
string removeID)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif        
dot.gif{
InBlock.gif            BindToDropDownList(ddlGoodsType, removeID,
null
true);
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockStart.gif
ContractedSubBlock.gif        
/**/
///
 
<summary>
InBlock.gif        
///
 绑定连动级的下拉菜单
InBlock.gif        
///
 
</summary>
InBlock.gif        
///
 
<param name="ddlGoodsType">
传进一个被绑定的DropDownList
</param>
InBlock.gif        
///
 
<param name="removeID">
被排除的ID,若没有,传null
</param>
ExpandedSubBlockEnd.gif        
///
 
<param name="parentID">
起始父ID
</param>
InBlock.gif        
public 
void BindToDropDownList(DropDownList ddlGoodsType, 
string removeID,
string parentID)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif        
dot.gif{
InBlock.gif            BindToDropDownList(ddlGoodsType, removeID,parentID, 
true);
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockEnd.gif        
#endregion
ExpandedSubBlockEnd.gif    }
ExpandedBlockEnd.gif}
调用方法很简单:
1.继承自IDropDownTree接口
2.实现3个接口方法
实现接口代码示例[Dispose方法自己实现],最主要的是自己实现获得子级的方法
ContractedBlock.gif
ExpandedBlockStart.gif 
IDropDownTree 成员
#region IDropDownTree 成员
InBlock.gif
InBlock.gif        
public Dictionary<
string
string> GetChildCategory(
string parentID)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string 
where = 
"
ParentID='
" + parentID + 
"
'
";
InBlock.gif            
if (
string.IsNullOrEmpty(parentID))
ExpandedSubBlockStart.gif
ContractedSubBlock.gif            
dot.gif{
InBlock.gif                
where = 
"
ParentID is null or ParentID='
" + Guid.Empty + 
"
'
";
ExpandedSubBlockEnd.gif            }
InBlock.gif            List<GoodsCategoryBean> _GoodsCategoryList = SelectList(
0
where
string.Empty, 
false);
InBlock.gif            
if (_GoodsCategoryList != 
null && _GoodsCategoryList.Count > 
0)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif            
dot.gif{
InBlock.gif                Dictionary<
string
string> categoryList = 
new Dictionary<
string
string>();
InBlock.gif                
for (
int i = 
0; i < _GoodsCategoryList.Count; i++)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif                
dot.gif{
InBlock.gif                    categoryList.Add(_GoodsCategoryList[i].ID.ToString(), _GoodsCategoryList[i].GategoryName);
ExpandedSubBlockEnd.gif                }
InBlock.gif                
return categoryList;
ExpandedSubBlockEnd.gif            }
InBlock.gif            
return 
null;
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif        
public Interface.Common.DropDownTree DropDownTree
ExpandedSubBlockStart.gif
ContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gif
ContractedSubBlock.gif            
get 
dot.gif
return 
new Interface.Common.DropDownTree(
this); }
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedBlockEnd.gif        
#endregion
页面调用代码: 类名.DropDownTree.BindToDropDownList(下拉控件ID);
希望对大伙有点帮助....
你可能感兴趣的文章
重置密码、单用户模式、救援模式
查看>>
LAMP环境搭建1-mysql5.5
查看>>
第三课 Linux目录及文件管理、用户组管理及bash重定向
查看>>
shell 脚本攻略--小试牛刀
查看>>
spring boot view override
查看>>
bzoj 2282: [Sdoi2011]消防
查看>>
我的友情链接
查看>>
centos5.9使用RPM包搭建lamp平台
查看>>
关于C#面向对象2
查看>>
Javascript String类的属性及方法
查看>>
vim编辑器如何添加或删除多行注释
查看>>
[LeetCode] Merge Intervals
查看>>
iOS开发-按钮的基本使用
查看>>
在QT和SDL搭建的框架中使用OPENGL在SDL窗口上进行绘图
查看>>
REST技术第三步 @BeanParam的使用
查看>>
SharePoint 读取 Site Columns 的数据并绑定到DropdownList
查看>>
Python中的对象行为与特殊方法(二)类型检查与抽象基类
查看>>
使用 axios 详解
查看>>
通信基站(dfs回溯,思维)
查看>>
nginx web加密访问
查看>>