很多的時候需要把枚舉作為數據源綁定DropDownList,一直簡單的做法是:

/**//// <summary>
/// 排序方向下拉列表,從DropDownList繼承
/// </summary>
public class SortOrderDropDownList : DropDownList
{
public SortOrderDropDownList() 
{
Items.Add(new ListItem("降序", ((int) SortOrder.Descending).ToString()));
Items.Add(new ListItem("升序", ((int) SortOrder.Ascending).ToString()));
}

/**//// <summary>
/// 自定義DropDownList的屬性SelectedValue
/// </summary>
public new SortOrder SelectedValue 
{
get
{ return (SortOrder) int.Parse(base.SelectedValue); }
set 
{
Items.FindByValue( base.SelectedValue ).Selected = false;
Items.FindByValue( ((int) value).ToString() ).Selected = true;
}
}
}用的時候呢,和普通的DropDownList是一樣的,這裡就不贅述了.
public enum Folk
{
[Description("漢族")]
HanZu,
[Description("回族")]
Huizi,
[Description("蒙古族")]
Menggu,
[Description("朝鮮族")]
ChaoXian,
[Description("他們族")]
TaMen
}
/**//// <summary>
/// 從枚舉類型和它的特性讀出並返回一個鍵值對
/// </summary>
/// <param name="enumType">Type,該參數的格式為typeof(需要讀的枚舉類型)</param>
/// <returns>鍵值對</returns>
public static NameValueCollection GetNVCFromEnumValue(Type enumType)
{
NameValueCollection nvc = new NameValueCollection();
Type typeDescription = typeof(DescriptionAttribute);
System.Reflection.FieldInfo[] fields = enumType.GetFIElds();
string strText = string.Empty;
string strValue = string.Empty;
foreach(FieldInfo field in fIElds)
{
if(field.FIEldType.IsEnum == true)
{
strValue = ((int)enumType.InvokeMember(field.Name,BindingFlags.GetFIEld,null,null,null)).ToString();
object[] arr = fIEld.GetCustomAttributes(typeDescription,true);
if(arr.Length > 0)
{
DescriptionAttribute aa = (DescriptionAttribute)arr[0];
strText = aa.Description;
}
else
{
strText = fIEld.Name;
}
nvc.Add(strText,strValue);
}
}
return nvc;
}

/**//// <summary>
/// 從枚舉類型和它的特性說明及枚舉值取得該枚舉的特性說明字符串
/// </summary>
/// <param name="enumType">枚舉類型typeof(需要讀的枚舉類型)</param>
/// <param name="index">枚舉的值</param>
/// <returns>枚舉的特性說明</returns>
public static string GetDescriptionFromNVC(Type enumType,int index)
{
string description = string.Empty;
NameValueCollection nvc = GetNVCFromEnumValue(enumType);
description = nvc.Keys[index];
return description;
}到了這裡,綁定起來相對就容易一點了.代碼如下,大家都比較反對僅僅羅列代碼的文章,但有些問題似乎是只有代碼能說的明白一點,所以只有再帖一點了:-)
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(); }
}
}至於用的時候呢,也比較簡單了.代碼中加了一些說明可以參考一下.
private void Page_Load(object sender, System.EventArgs e)
{
this.flokthis.AutoPostBack = true;
this.DataBind();
NameValueCollection nvc = GetNVCFromEnumValue(typeof(Folk));
Response.Write(this.flokthis.SelectedValue.ToString());
Response.Write(nvc.Keys[int.Parse(flokthis.SelectedValue)]);
//如果從數據庫中讀出的如0.1.2.3等的值,取得它對應的枚舉的特性說明.
string testNVCvoid = string.Empty;
Response.Write(GetDescriptionFromNVC(typeof(Folk),int.Parse(flokthis.SelectedValue)));
}到這裡,問題解決了.我的文章也完了.