在项目中经常遇到树状结构的对象比如产品分类、部门结构、地区……对于这类对象的呈现,一般都使用树控件(比如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;
![](/Images/OutliningIndicators/None.gif)
namespace ServerControls
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
[ToolboxData("<{0}:DropDownTreeList runat=server></{0}:DropDownTreeList>")]
public class DropDownTreeList : DropDownList
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private object dataSource;
private int deep = 0;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
----重写----#region ----重写----
public override object DataSource
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.dataSource;
}
set
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.dataSource = value;
}
}
public override void DataBind()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (ChildProperty == null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new Exception("ChildProperty参数必须设置");
}
this.Items.Clear();
ListItemCollection items=ConvertTreeToList(dataSource);
foreach (ListItem item in items)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Items.Add(item);
}
![](/Images/OutliningIndicators/InBlock.gif)
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
----私有方法----#region ----私有方法----
private ListItemCollection ConvertTreeToList(object root)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
deep = 0;
ListItemCollection list = new ListItemCollection();
list.Add(GetListItem(root));
ConvertTree(list, root);
return list;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 将对象转换为ListItem
/// </summary>
/// <param name="root"></param>
/// <returns></returns>
private ListItem GetListItem(object root)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
object childs= root.GetType().GetProperty(this.ChildProperty).GetValue(root,null);//获得Child的集合
if(childs==null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return;
}
if(!(childs is ICollection))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new Exception("数据源的"+ChildProperty+"属性必须实现ICollection接口");
}
deep++;
foreach(object child in (ICollection)childs)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
list.Add(GetListItem(child));
ConvertTree(list, child);//递归转换下一层节点
}
deep--;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 根据节点的深度返回节点前的占位字符
/// </summary>
/// <returns></returns>
private string GetDeepChar()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string str = "";
for (int i = 0; i < deep; i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
str += DeepChar;
}
return str;
![](/Images/OutliningIndicators/InBlock.gif)
}
#endregion
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
----公开的属性----#region ----公开的属性----
[Description("表示深度增加的字符")]
public string DeepChar
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (ViewState["DeepChar"] == null || ViewState["DeepChar"].ToString()=="")
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return "--";
}
return ViewState["DeepChar"].ToString();
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ ViewState["DeepChar"] = value; }
}
[Description("对象的子节点集合属性名")]
public string ChildProperty
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (ViewState["ChildProperty"] == null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return null;
}
return ViewState["ChildProperty"].ToString();
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
protected void Page_Load(object sender, EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.DropDownTreeList1.DataSource = InitArea();
this.DropDownTreeList1.DataBind();
}
![](/Images/OutliningIndicators/InBlock.gif)
private Area InitArea()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Area area1 = new Area
{ Code = 1, Name = "中国" };
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Area area2 = new Area
{ Code = 2, Name = "四川" };
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Area area3 = new Area
{ Code = 3, Name = "北京" };
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Area area4 = new Area
{ Code = 4, Name = "广东" };
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Area area5 = new Area
{ Code = 5, Name = "成都" };
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Area area6 = new Area
{ Code = 6, Name = "乐山" };
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Area area7 = new Area
{ Code = 7, Name = "绵阳" };
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Area area8 = new Area
{ Code = 8, Name = "广州" };
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Area area9 = new Area
{ Code = 9, Name = "深圳" };
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Area area10 = new Area
{ Code = 10, Name = "东莞" };
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Area area11 = new Area
{ Code = 11, Name = "珠江" };
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
area1.ChildArea = new List<Area>
{ area2, area3, area4 };
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
area2.ChildArea = new List<Area>
{ area5, area6, area7 };
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
area4.ChildArea = new List<Area>
{ area9, area10, area11 };
return area1;
}
}
public class Area
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public string Name
{ get; set; }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public int Code
{ get; set; }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public List<Area> ChildArea
{ get; set; }
}
![](/Images/OutliningIndicators/None.gif)
运行后的效果就是: 中国--四川----成都----乐山----绵阳--北京--广东----深圳----东莞----珠江
由于本人接触服务器控件开发的时间并不长,对服务器控件开发经验还不足,该控件有需要改进或有错误的地方希望大家指正。
另外这个控件目前我发现的一个问题就是我将DataBind方法完全重写了,所以控件的DataTextFormatString失效了,由于很少使用这个属性,所以一般不影响使用。不知道有什么办法能够解决这个问题?