发现项目中很多采用了硬编码的方式,去绑定下拉框数据,用法相当地挫。之前没有去写个程序梳理下,于是,稍微实现了下通用的绑定方式。
有这样一个学科枚举类型:


///
<summary>
/// 学科
/// </summary>
public enum Subject
{
None = 0 ,
[Description( " 语文 " )]
Chinese = 1 ,
[Description( " 数学 " )]
Mathematics = 2 ,
[Description( " 英语 " )]
English = 3 ,
[Description( " 政治 " )]
Politics = 4 ,
[Description( " 物理 " )]
Physics = 5 ,
[Description( " 化学 " )]
Chemistry = 6 ,
[Description( " 历史 " )]
History = 7 ,
[Description( " 地理 " )]
Geography = 8 ,
[Description( " 生物 " )]
Biology = 9
}
/// 学科
/// </summary>
public enum Subject
{
None = 0 ,
[Description( " 语文 " )]
Chinese = 1 ,
[Description( " 数学 " )]
Mathematics = 2 ,
[Description( " 英语 " )]
English = 3 ,
[Description( " 政治 " )]
Politics = 4 ,
[Description( " 物理 " )]
Physics = 5 ,
[Description( " 化学 " )]
Chemistry = 6 ,
[Description( " 历史 " )]
History = 7 ,
[Description( " 地理 " )]
Geography = 8 ,
[Description( " 生物 " )]
Biology = 9
}
这里使用了一个Description特性,目的是为了在一个DropDownList列表中绑定文本数据。
这里使用了一个扩展方法,目的为了返回一个Value和Text的数据列表,上方法:


///
<summary>
/// 枚举辅助类
/// </summary>
public static class EnumHelper
{
/// <summary>
/// 获得枚举类型数据项(不包括空项)
/// </summary>
/// <param name="enumType"> 枚举类型 </param>
/// <returns></returns>
public static IList < object > GetItems( this Type enumType)
{
if ( ! enumType.IsEnum)
throw new InvalidOperationException();
IList < object > list = new List < object > ();
// 获取Description特性
Type typeDescription = typeof (DescriptionAttribute);
// 获取枚举字段
FieldInfo[] fields = enumType.GetFields();
foreach (FieldInfo field in fields)
{
if ( ! field.FieldType.IsEnum)
continue ;
// 获取枚举值
int value = ( int )enumType.InvokeMember(field.Name, BindingFlags.GetField, null , null , null );
// 不包括空项
if (value > 0 )
{
string text = string .Empty;
object [] array = field.GetCustomAttributes(typeDescription, false );
if (array.Length > 0 ) text = ((DescriptionAttribute)array[ 0 ]).Description;
else text = field.Name; // 没有描述,直接取值
// 添加到列表
list.Add( new { Value = value, Text = text });
}
}
return list;
}
}
/// 枚举辅助类
/// </summary>
public static class EnumHelper
{
/// <summary>
/// 获得枚举类型数据项(不包括空项)
/// </summary>
/// <param name="enumType"> 枚举类型 </param>
/// <returns></returns>
public static IList < object > GetItems( this Type enumType)
{
if ( ! enumType.IsEnum)
throw new InvalidOperationException();
IList < object > list = new List < object > ();
// 获取Description特性
Type typeDescription = typeof (DescriptionAttribute);
// 获取枚举字段
FieldInfo[] fields = enumType.GetFields();
foreach (FieldInfo field in fields)
{
if ( ! field.FieldType.IsEnum)
continue ;
// 获取枚举值
int value = ( int )enumType.InvokeMember(field.Name, BindingFlags.GetField, null , null , null );
// 不包括空项
if (value > 0 )
{
string text = string .Empty;
object [] array = field.GetCustomAttributes(typeDescription, false );
if (array.Length > 0 ) text = ((DescriptionAttribute)array[ 0 ]).Description;
else text = field.Name; // 没有描述,直接取值
// 添加到列表
list.Add( new { Value = value, Text = text });
}
}
return list;
}
}
这里采用特性反射的方式得到了对应的Value和Text,最后返回了一个new { Value = …, Text = … }的匿名类的列表。
那么页面上实现就相当简单了:


<
asp:DropDownList ID
=
"
ddlSubject
"
runat
=
"
server
"
></
asp:DropDownList
>
ddlSubject.DataSource = typeof (Subject).GetItems();
ddlSubject.DataTextField = " Text " ;
ddlSubject.DataValueField = " Value " ;
ddlSubject.DataBind();
ddlSubject.DataSource = typeof (Subject).GetItems();
ddlSubject.DataTextField = " Text " ;
ddlSubject.DataValueField = " Value " ;
ddlSubject.DataBind();
最后得到一个绑定学科的下拉列表:
这,非常简单:)
PS:全国资源最全最有效的高中教育学习网站:http://edu.591up.com/