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
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是一样的,这里就不赘述了.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
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 }
今天做项目遇到了一个新的情况,需要一个民族选择的下拉菜单,这下可有点麻烦了,56个民族啊,那不是要写56行一样的东东?#$%^&*.想用Html控件,把值全部写在Html里面吧,还是觉得有点麻烦.想到了一个原来的同事阿哲写的一篇用反射+特性列出所有的枚举变量及其描述信息,绑定到DropDownList上,参考了一下,这个思路真好~!下面看看我的:
定义枚举的时候如上文的终极解决方案所述,将枚举定义加上它的特性说明,如本例所用到的民族枚举:
public enum Folk
{
[Description("汉族")]
HanZu,
[Description("回族")]
Huizi,
[Description("蒙古族")]
Menggu,
[Description("朝鲜族")]
ChaoXian,
[Description("他们族")]
TaMen
}
{
[Description("汉族")]
HanZu,
[Description("回族")]
Huizi,
[Description("蒙古族")]
Menggu,
[Description("朝鲜族")]
ChaoXian,
[Description("他们族")]
TaMen
}
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 }
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 }
这个方法可以看看阿哲的文章中的说明,讲的很清楚了.不同的是一个是DataTable,一个是NameValueCollection而已.
2.从枚举类型和它的特性说明及枚举值取得该枚举的特性说明字符串
1
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 }
到了这里,绑定起来相对就容易一点了.代码如下,大家都比较反对仅仅罗列代码的文章,但有些问题似乎是只有代码能说的明白一点,所以只有再帖一点了:-)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 }
public class FolkDropDownList : DropDownList
{
public FolkDropDownList(){}
public override void DataBind()
{
base.DataBind ();
if(!Page.IsPostBack)
{
NameValueCollection nvc = GetNVCFromEnumValue(typeof(Folk));
for(int i = 0;i < nvc.Count;i++)
{
Items.Add(new ListItem(nvc.Keys[i],nvc[i]));
}
}
}
public new Folk SelectedValue
{
get { return (Folk) int.Parse(base.SelectedValue); }
set { base.SelectedValue = ((int) value).ToString(); }
}
}
至于用的时候呢,也比较简单了.代码中加了一些说明可以参考一下.{
public FolkDropDownList(){}
public override void DataBind()
{
base.DataBind ();
if(!Page.IsPostBack)
{
NameValueCollection nvc = GetNVCFromEnumValue(typeof(Folk));
for(int i = 0;i < nvc.Count;i++)
{
Items.Add(new ListItem(nvc.Keys[i],nvc[i]));
}
}
}
public new Folk SelectedValue
{
get { return (Folk) int.Parse(base.SelectedValue); }
set { base.SelectedValue = ((int) value).ToString(); }
}
}
1 private void Page_Load(object sender, System.EventArgs e)
2 {
//测试用,将该DropDownList的AutoPostBack设为true.
3 this.flokthis.AutoPostBack = true;
4 this.DataBind();
//这里就从民族枚举中取得键值对,当选择一项的时候读出所选的值 5
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 }
到这里,问题解决了.我的文章也完了.2 {
//测试用,将该DropDownList的AutoPostBack设为true.
3 this.flokthis.AutoPostBack = true;
4 this.DataBind();
//这里就从民族枚举中取得键值对,当选择一项的时候读出所选的值 5
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 }
祝大家HappyEveryDay.MoneyMoreMore,GirlGoodGood.
作者:vincent zhang
出处:http://ode.cnblogs.com http://odevincent.blog.51cto.com
Email:wensheng.zhang#postgres.cn
本作品由vincent zh创作,采用知识共享署名-非商业性使用-禁止演绎 3.0 中国大陆许可协议进行许可。