在项目中经常遇到树状结构的对象比如产品分类、部门结构、地区……对于这类对象的呈现,一般都使用树控件(比如VS2005自带的TreeView控件)。但是树控件的使用和操作都比较复杂,对于一些比较简单的操作,比如单选其中的一个节点的情况则可用使用下拉列表框来代替。要在DropDownList中展示出树结构的层次,那就必须在每个节点的Text前加入一定的占位符,以实现层次的效果,比如:
由于这种下拉列表控件在项目中经常使用,于是决定写一个通用的服务器控件出来。该控件继承自DropDownList,在使用中只需要为该控件设置用于数据绑定的DataTextField和DataValueField,以及新增的属性ChildProperty(string,对象的Child属性的名字)和DeepChar(string,在表示层次中使用的占位符,默认是“--”),设置了这4个属性后,在后台就只需要将树结构对象的Root节点作为DataSource,然后执行DataBind()即可。

控件Code
using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ServerControls


{
[ToolboxData("<{0}:DropDownTreeList runat=server></{0}:DropDownTreeList>")]
public class DropDownTreeList : DropDownList

{
private object dataSource;
private int deep = 0;


----重写----#region ----重写----
public override object DataSource

{
get

{
return this.dataSource;
}
set

{
this.dataSource = value;
}
}
public override void DataBind()

{
if (ChildProperty == null)

{
throw new Exception("ChildProperty参数必须设置");
}
this.Items.Clear();
ListItemCollection items=ConvertTreeToList(dataSource);
foreach (ListItem item in items)

{
this.Items.Add(item);
}

}
#endregion


----私有方法----#region ----私有方法----
private ListItemCollection ConvertTreeToList(object root)

{
deep = 0;
ListItemCollection list = new ListItemCollection();
list.Add(GetListItem(root));
ConvertTree(list, root);
return list;
}

/**//// <summary>
/// 将对象转换为ListItem
/// </summary>
/// <param name="root"></param>
/// <returns></returns>
private ListItem GetListItem(object root)

{
ListItem item = new ListItem();
item.Text =GetDeepChar()+ root.GetType().GetProperty(this.DataTextField).GetValue(root, null).ToString();
item.Value = root.GetType().GetProperty(this.DataValueField).GetValue(root, null).ToString();
return item;
}
private void ConvertTree(ListItemCollection list, object root)

{
object childs= root.GetType().GetProperty(this.ChildProperty).GetValue(root,null);//获得Child的集合
if(childs==null)

{
return;
}
if(!(childs is ICollection))

{
throw new Exception("数据源的"+ChildProperty+"属性必须实现ICollection接口");
}
deep++;
foreach(object child in (ICollection)childs)

{

list.Add(GetListItem(child));
ConvertTree(list, child);//递归转换下一层节点
}
deep--;
}

/**//// <summary>
/// 根据节点的深度返回节点前的占位字符
/// </summary>
/// <returns></returns>
private string GetDeepChar()

{
string str = "";
for (int i = 0; i < deep; i++)

{
str += DeepChar;
}
return str;

}
#endregion

----公开的属性----#region ----公开的属性----
[Description("表示深度增加的字符")]
public string DeepChar

{
get

{
if (ViewState["DeepChar"] == null || ViewState["DeepChar"].ToString()=="")

{
return "--";
}
return ViewState["DeepChar"].ToString();
}

set
{ ViewState["DeepChar"] = value; }
}
[Description("对象的子节点集合属性名")]
public string ChildProperty

{
get

{
if (ViewState["ChildProperty"] == null)

{
return null;
}
return ViewState["ChildProperty"].ToString();
}

set
{ ViewState["ChildProperty"] = value; }
}
#endregion
}
}
具体调用示例:
1,将该控件添加到aspx页面中并设置必要的属性。
<cc1:DropDownTreeList ID="DropDownTreeList1" runat="server"
ChildProperty="ChildArea" DataTextField="Name" DataValueField="Code"
DeepChar="--">
2,在页面的后台代码中添加数据源并进行数据绑定。

后台数据绑定
public partial class _Default : System.Web.UI.Page


{
protected void Page_Load(object sender, EventArgs e)

{
this.DropDownTreeList1.DataSource = InitArea();
this.DropDownTreeList1.DataBind();
}

private Area InitArea()

{

Area area1 = new Area
{ Code = 1, Name = "中国" };

Area area2 = new Area
{ Code = 2, Name = "四川" };

Area area3 = new Area
{ Code = 3, Name = "北京" };

Area area4 = new Area
{ Code = 4, Name = "广东" };

Area area5 = new Area
{ Code = 5, Name = "成都" };

Area area6 = new Area
{ Code = 6, Name = "乐山" };

Area area7 = new Area
{ Code = 7, Name = "绵阳" };

Area area8 = new Area
{ Code = 8, Name = "广州" };

Area area9 = new Area
{ Code = 9, Name = "深圳" };

Area area10 = new Area
{ Code = 10, Name =