好多年没写文章了
这里就分享点自己原创的一点破代码,效果如图下:
![](/images/cnblogs_com/cyq1162/112694/r_{DB83491B-1991-48D4-9A9B-69704E20B45C}0.jpg)
本人的提供的代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
![](/Images/OutliningIndicators/None.gif)
namespace Interface.Common
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
public interface IDropDownTree : IDisposable
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 返回Dictionary里分别对应ID,文本,如果没有子节点返回null
/// </summary>
/// <param name="parentID">父节点ID</param>
/// <returns></returns>
Dictionary<string, string> GetChildCategory(string parentID);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 代码里写return new Interface.Common.DropDownTree(this);
/// </summary>
DropDownTree DropDownTree
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get;
}
}
public sealed class DropDownTree
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
IDropDownTree _DropDownTree;
public DropDownTree(IDropDownTree dropDownTree)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_DropDownTree = dropDownTree;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 用于树的前缀
/// </summary>
/// <param name="IsLast">是否是同级节点中的最后一个</param>
/// <param name="HasChild">本节点是否拥有子节点</param>
/// <param name="ParentString">父节点前缀符号</param>
/// <returns>本节点的前缀</returns>
private string GetPreFix(bool isLast, bool hasChild, string parentString)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string result = string.Empty;
if (!string.IsNullOrEmpty(parentString))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
parentString = parentString.Remove(parentString.Length - 1).Replace("├", "│").Replace("└", " ");
result += parentString;
}
if (isLast)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
result += "└";
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
result += "├";
}
if (hasChild)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
result += "┬";
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
result += "─";
}
return result;
}
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
绑定下拉菜单#region 绑定下拉菜单
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 绑定连动级的下拉菜单
/// </summary>
/// <param name="ddlGoodsType">传进一个被绑定的DropDownList</param>
/// <param name="removeID">被排除绑定的节点ID</param>
/// <param name="AutoDispose">是否自动释放</param>
public void BindToDropDownList(DropDownList ddlGoodsType, string removeID,string parentID, bool autoDispose)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (ddlGoodsType != null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ListItem listItem = null;
string currentID = parentID;//根节点/父ID
string currentSign = string.Empty;//当前节点符号;
string parrentSign = string.Empty; //父节点符号;
bool HasChild = true;//是否有子
Queue<string> parentKeyList = new Queue<string>();//存 有子节点的 节点ID
Queue<string> parentSignList = new Queue<string>();//对应节点ID的前缀符号
int itemIndexOf = 0;//父节点所在的位置
while (HasChild)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int lastOneCount = 1;//用于计算在同级别中是否最后一个
Dictionary<string, string> childList = _DropDownTree.GetChildCategory(currentID);// 得到子节点列表
if (childList != null && childList.Count > 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!string.IsNullOrEmpty(removeID) && childList.ContainsKey(removeID))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
childList.Remove(removeID);
}
foreach (KeyValuePair<string, string> entry in childList)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (_DropDownTree.GetChildCategory(entry.Key) != null)//存在子
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
currentSign = GetPreFix(lastOneCount == childList.Count, true, parrentSign);
listItem = new ListItem(currentSign + entry.Value, entry.Key);
![](/Images/OutliningIndicators/InBlock.gif)
parentKeyList.Enqueue(entry.Key);//当前的节点ID
parentSignList.Enqueue(currentSign);//当前的节点符号
}
else//不存在子
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
currentSign = GetPreFix(lastOneCount == childList.Count, false, parrentSign);
listItem = new ListItem(currentSign + entry.Value, entry.Key);
}
if (ddlGoodsType.Items.Count != 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
itemIndexOf = string.IsNullOrEmpty(currentID) ? itemIndexOf + 1 : ddlGoodsType.Items.IndexOf(ddlGoodsType.Items.FindByValue(currentID)) + lastOneCount;
}
ddlGoodsType.Items.Insert(itemIndexOf, listItem);//添加子节点
lastOneCount++;
}
if (parentKeyList.Count > 0)//存在子节点时
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
currentID = parentKeyList.Dequeue();
parrentSign = parentSignList.Dequeue();
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
HasChild = false;
}
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
break;
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
}
if (autoDispose)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_DropDownTree.Dispose();
}
![](/Images/OutliningIndicators/InBlock.gif)
}
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 绑定连动级的下拉菜单
/// </summary>
/// <param name="ddlGoodsType">传进一个被绑定的DropDownList</param>
public void BindToDropDownList(DropDownList ddlGoodsType)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
BindToDropDownList(ddlGoodsType, string.Empty,null, true);
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 绑定连动级的下拉菜单
/// </summary>
/// <param name="ddlGoodsType">传进一个被绑定的DropDownList</param>
/// <param name="removeID">被排除的ID</param>
public void BindToDropDownList(DropDownList ddlGoodsType, string removeID)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
BindToDropDownList(ddlGoodsType, removeID,null, true);
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 绑定连动级的下拉菜单
/// </summary>
/// <param name="ddlGoodsType">传进一个被绑定的DropDownList</param>
/// <param name="removeID">被排除的ID,若没有,传null</param>
/// <param name="parentID">起始父ID</param>
public void BindToDropDownList(DropDownList ddlGoodsType, string removeID,string parentID)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
BindToDropDownList(ddlGoodsType, removeID,parentID, true);
}
#endregion
}
}调用方法很简单:
1.继承自IDropDownTree接口
2.实现3个接口方法
实现接口代码示例[Dispose方法自己实现],最主要的是自己实现获得子级的方法
![](/Images/OutliningIndicators/ContractedBlock.gif)
IDropDownTree 成员#region IDropDownTree 成员
![](/Images/OutliningIndicators/InBlock.gif)
public Dictionary<string, string> GetChildCategory(string parentID)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string where = "ParentID='" + parentID + "'";
if (string.IsNullOrEmpty(parentID))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
where = "ParentID is null or ParentID='" + Guid.Empty + "'";
}
List<GoodsCategoryBean> _GoodsCategoryList = SelectList(0, where, string.Empty, false);
if (_GoodsCategoryList != null && _GoodsCategoryList.Count > 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Dictionary<string, string> categoryList = new Dictionary<string, string>();
for (int i = 0; i < _GoodsCategoryList.Count; i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
categoryList.Add(_GoodsCategoryList[i].ID.ToString(), _GoodsCategoryList[i].GategoryName);
}
return categoryList;
}
return null;
}
![](/Images/OutliningIndicators/InBlock.gif)
public Interface.Common.DropDownTree DropDownTree
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return new Interface.Common.DropDownTree(this); }
}
![](/Images/OutliningIndicators/InBlock.gif)
#endregion页面调用代码: 类名.DropDownTree.BindToDropDownList(下拉控件ID);
希望对大伙有点帮助....