这篇文章主要介绍介绍asp.net项目开发中枚举的使用。包含了显示枚举的值和为下拉框绑定枚举两个功能举例说明。
1 显示枚举的值:
2 为下拉框绑定枚举:
软件介绍:金戈企业建站系统不仅是一份免费的企业建站代码包,而且它还是完全开源的,它倾注了作者1个多月来日日夜夜的心血,虽然有些地方没做到尽善尽美,可我相信在接下来的日子里我会通过反馈信息让她更丰满实用起来。1.完美的摸板机制,即使你对php一点也不懂,只要你会做网页。就可以立即打造新颖别致的网站界面(摸板制作方法手册正在紧张制作中,稍后发布)可惜作者精力有限,目前只提供一套摸板。不过只是暂时的2.
GetEnumList(ddlBids);
void GetEnumList(DropDownList ddl)
{
foreach (EnumBidCardType s in System.Enum.GetValues(typeof(EnumBidCardType)))
{
ddl.Items.Add(new ListItem(s.ToString(), ((int)s).ToString()));
}
}
this.ddlBids.DataSource = GetEnumList(typeof(EnumBidCardType), true);
this.ddlBids.DataTextField = "Text";
this.ddlBids.DataValueField = "Value";
this.ddlBids.DataBind();
public static List GetEnumList(Type enumType, bool allAllOption)
{
if (enumType.IsEnum == false)
{
return null;
}
List list = new List();
if (allAllOption == true)
{
list.Add(new ListItem("--全部--", ""));
}
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.IsSpecialName) continue;
strValue = field.GetRawConstantValue().ToString();
object[] arr = field.GetCustomAttributes(typeDescription, true);
if (arr.Length > 0)
{
strText = (arr[0] as DescriptionAttribute).Description;
}
else
{
strText = field.Name;
}
list.Add(new ListItem(strText, strValue));
}
return list;
}









