在项目中经常遇到树状结构的对象比如产品分类、部门结构、地区……对于这类对象的呈现,一般都使用树控件(比如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 = " 东莞 " } ; Area area11 = new Area { Code = 11 , Name = " 珠江 " } ; area1.ChildArea = new List < Area > { area2, area3, area4 } ; area2.ChildArea = new List < Area > { area5, area6, area7 } ; area4.ChildArea = new List < Area > { area9, area10, area11 } ; return area1; } } public class Area { public string Name { get ; set ; } public int Code { get ; set ; } public List < Area > ChildArea { get ; set ; } }
运行后的效果就是: 中国 --四川 ----成都 ----乐山 ----绵阳 --北京 --广东 ----深圳 ----东莞 ----珠江
由于本人接触服务器控件开发的时间并不长,对服务器控件开发经验还不足,该控件有需要改进或有错误的地方希望大家指正。
另外这个控件目前我发现的一个问题就是我将DataBind方法完全重写了,所以控件的DataTextFormatString失效了,由于很少使用这个属性,所以一般不影响使用。不知道有什么办法能够解决这个问题?