DropDownList绑定枚举类型
很多的时候需要把枚举作为数据源绑定DropDownList,一直简单的做法是:
1
/// <summary>
2
/// 排序方向下拉列表,从DropDownList继承
3
/// </summary>
4
public class SortOrderDropDownList : DropDownList
5
{
6
public SortOrderDropDownList()
7
{
8
Items.Add(new ListItem("降序", ((int) SortOrder.Descending).ToString()));
9
Items.Add(new ListItem("升序", ((int) SortOrder.Ascending).ToString()));
10
}
11![](/Images/OutliningIndicators/InBlock.gif)
12
/// <summary>
13
/// 自定义DropDownList的属性SelectedValue
14
/// </summary>
15
public new SortOrder SelectedValue
16
{
17
get { return (SortOrder) int.Parse(base.SelectedValue); }
18
set
19
{
20
Items.FindByValue( base.SelectedValue ).Selected = false;
21
Items.FindByValue( ((int) value).ToString() ).Selected = true;
22
}
23
}
24
}
用的时候呢,和普通的DropDownList是一样的,这里就不赘述了.![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](/Images/OutliningIndicators/InBlock.gif)
3
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
17
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
23
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
24
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
今天做项目遇到了一个新的情况,需要一个民族选择的下拉菜单,这下可有点麻烦了,56个民族啊,那不是要写56行一样的东东?#$%^&*.想用Html控件,把值全部写在Html里面吧,还是觉得有点麻烦.想到了一个原来的同事阿哲写的一篇用反射+特性列出所有的枚举变量及其描述信息,绑定到DropDownList上,参考了一下,这个思路真好~!下面看看我的:
定义枚举的时候如上文的终极解决方案所述,将枚举定义加上它的特性说明,如本例所用到的民族枚举:
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
1.从枚举类型和它的特性读出并返回一个键值对
1
/// <summary>
2
/// 从枚举类型和它的特性读出并返回一个键值对
3
/// </summary>
4
/// <param name="enumType">Type,该参数的格式为typeof(需要读的枚举类型)</param>
5
/// <returns>键值对</returns>
6
public static NameValueCollection GetNVCFromEnumValue(Type enumType)
7
{
8
NameValueCollection nvc = new NameValueCollection();
9
Type typeDescription = typeof(DescriptionAttribute);
10
System.Reflection.FieldInfo[] fields = enumType.GetFields();
11
string strText = string.Empty;
12
string strValue = string.Empty;
13
foreach(FieldInfo field in fields)
14
{
15
if(field.FieldType.IsEnum == true)
16
{
17
strValue = ((int)enumType.InvokeMember(field.Name,BindingFlags.GetField,null,null,null)).ToString();
18
object[] arr = field.GetCustomAttributes(typeDescription,true);
19
if(arr.Length > 0)
20
{
21
DescriptionAttribute aa = (DescriptionAttribute)arr[0];
22
strText = aa.Description;
23
}
24
else
25
{
26
strText = field.Name;
27
}
28
nvc.Add(strText,strValue);
29
}
30
}
31
return nvc;
32
}
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](/Images/OutliningIndicators/InBlock.gif)
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/InBlock.gif)
5
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
30
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
32
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
这个方法可以看看阿哲的文章中的说明,讲的很清楚了.不同的是一个是DataTable,一个是NameValueCollection而已.
2.从枚举类型和它的特性说明及枚举值取得该枚举的特性说明字符串
1![](/Images/OutliningIndicators/None.gif)
2
/// <summary>
3
/// 从枚举类型和它的特性说明及枚举值取得该枚举的特性说明字符串
4
/// </summary>
5
/// <param name="enumType">枚举类型typeof(需要读的枚举类型)</param>
6
/// <param name="index">枚举的值</param>
7
/// <returns>枚举的特性说明</returns>
8
public static string GetDescriptionFromNVC(Type enumType,int index)
9
{
10
string description = string.Empty;
11
NameValueCollection nvc = GetNVCFromEnumValue(enumType);
12
description = nvc.Keys[index];
13
return description;
14
}
到了这里,绑定起来相对就容易一点了.代码如下,大家都比较反对仅仅罗列代码的文章,但有些问题似乎是只有代码能说的明白一点,所以只有再帖一点了:-)![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/InBlock.gif)
5
![](/Images/OutliningIndicators/InBlock.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
8
![](/Images/OutliningIndicators/None.gif)
9
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
1
private void Page_Load(object sender, System.EventArgs e)
2
{
//测试用,将该DropDownList的AutoPostBack设为true.
3
this.flokthis.AutoPostBack = true;
4
this.DataBind();
//这里就从民族枚举中取得键值对,当选择一项的时候读出所选的值 5![](/Images/OutliningIndicators/InBlock.gif)
6
NameValueCollection nvc = GetNVCFromEnumValue(typeof(Folk));
7
Response.Write(this.flokthis.SelectedValue.ToString());
//这里读出所选枚举的特性说明字符串
9
Response.Write(nvc.Keys[int.Parse(flokthis.SelectedValue)]);
10
//如果从数据库中读出的如0.1.2.3等的值,取得它对应的枚举的特性说明.
11
string testNVCvoid = string.Empty;
12
Response.Write(GetDescriptionFromNVC(typeof(Folk),int.Parse(flokthis.SelectedValue)));
13
}
到这里,问题解决了.我的文章也完了.![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
//测试用,将该DropDownList的AutoPostBack设为true.
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/InBlock.gif)
//这里就从民族枚举中取得键值对,当选择一项的时候读出所选的值 5
![](/Images/OutliningIndicators/InBlock.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
//这里读出所选枚举的特性说明字符串
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
祝大家HappyEveryDay.MoneyMoreMore,GirlGoodGood.
作者:vincent zhang
出处:http://ode.cnblogs.com http://odevincent.blog.51cto.com
Email:wensheng.zhang#postgres.cn
![知识共享许可协议](http://i.creativecommons.org/l/by-nc-nd/3.0/cn/88x31.png)
本作品由vincent zh创作,采用知识共享署名-非商业性使用-禁止演绎 3.0 中国大陆许可协议进行许可。