好多年沒寫文章了
這裡就分享點自己原創的一點破代碼,效果如圖下:
本人的提供的代碼如下:
using system;
using system.collections.generic;
using system.text;
using system.web.ui.webcontrols;
namespace interface.common
{
public interface idropdowntree : idisposable
{
/// <summary>
/// 傳回dictionary裡分别對應id,文本,如果沒有子節點傳回null
/// </summary>
/// <param name="parentid">父節點id</param>
/// <returns></returns>
dictionary<string, string> getchildcategory(string parentid);
/// 代碼裡寫return new interface.common.dropdowntree(this);
dropdowntree dropdowntree
{
get;
}
}
public sealed class dropdowntree
idropdowntree _dropdowntree;
public dropdowntree(idropdowntree dropdowntree)
_dropdowntree = dropdowntree;
/// 用于樹的字首
/// <param name="islast">是否是同級節點中的最後一個</param>
/// <param name="haschild">本節點是否擁有子節點</param>
/// <param name="parentstring">父節點字首符号</param>
/// <returns>本節點的字首</returns>
private string getprefix(bool islast, bool haschild, string parentstring)
string result = string.empty;
if (!string.isnullorempty(parentstring))
{
parentstring = parentstring.remove(parentstring.length - 1).replace("├", "│").replace("└", " ");
result += parentstring;
}
if (islast)
result += "└";
else
result += "├";
if (haschild)
result += "┬";
result += "─";
return result;
#region 綁定下拉菜單
/// 綁定連動級的下拉菜單
/// <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)
if (ddlgoodstype != null)
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)
{
int lastonecount = 1;//用于計算在同級别中是否最後一個
dictionary<string, string> childlist = _dropdowntree.getchildcategory(currentid);// 得到子節點清單
if (childlist != null && childlist.count > 0)
{
if (!string.isnullorempty(removeid) && childlist.containskey(removeid))
{
childlist.remove(removeid);
}
foreach (keyvaluepair<string, string> entry in childlist)
if (_dropdowntree.getchildcategory(entry.key) != null)//存在子
{
currentsign = getprefix(lastonecount == childlist.count, true, parrentsign);
listitem = new listitem(currentsign + entry.value, entry.key);
parentkeylist.enqueue(entry.key);//目前的節點id
parentsignlist.enqueue(currentsign);//目前的節點符号
}
else//不存在子
currentsign = getprefix(lastonecount == childlist.count, false, parrentsign);
if (ddlgoodstype.items.count != 0)
itemindexof = string.isnullorempty(currentid) ? itemindexof + 1 : ddlgoodstype.items.indexof(ddlgoodstype.items.findbyvalue(currentid)) + lastonecount;
ddlgoodstype.items.insert(itemindexof, listitem);//添加子節點
lastonecount++;
if (parentkeylist.count > 0)//存在子節點時
currentid = parentkeylist.dequeue();
parrentsign = parentsignlist.dequeue();
else
haschild = false;
}
else
break;
}
if (autodispose)
_dropdowntree.dispose();
public void bindtodropdownlist(dropdownlist ddlgoodstype)
bindtodropdownlist(ddlgoodstype, string.empty,null, true);
/// <param name="removeid">被排除的id</param>
public void bindtodropdownlist(dropdownlist ddlgoodstype, string removeid)
bindtodropdownlist(ddlgoodstype, removeid,null, true);
/// <param name="removeid">被排除的id,若沒有,傳null</param>
/// <param name="parentid">起始父id</param>
public void bindtodropdownlist(dropdownlist ddlgoodstype, string removeid,string parentid)
bindtodropdownlist(ddlgoodstype, removeid,parentid, true);
#endregion
}
調用方法很簡單:
1.繼承自idropdowntree接口
2.實作3個接口方法
實作接口代碼示例[dispose方法自己實作],最主要的是自己實作獲得子級的方法
#region idropdowntree 成員
public dictionary<string, string> getchildcategory(string parentid)
string where = "parentid='" + parentid + "'";
if (string.isnullorempty(parentid))
where = "parentid is null or parentid='" + guid.empty + "'";
list<goodscategorybean> _goodscategorylist = selectlist(0, where, string.empty, false);
if (_goodscategorylist != null && _goodscategorylist.count > 0)
dictionary<string, string> categorylist = new dictionary<string, string>();
for (int i = 0; i < _goodscategorylist.count; i++)
categorylist.add(_goodscategorylist[i].id.tostring(), _goodscategorylist[i].gategoryname);
return categorylist;
return null;
public interface.common.dropdowntree dropdowntree
get { return new interface.common.dropdowntree(this); }
頁面調用代碼: 類名.dropdowntree.bindtodropdownlist(下拉控件id);
希望對大夥有點幫助....