ASP.NET 下拉列表绑定枚举类型值,不用再新建一个枚举表

时间:2022-09-19 23:10:14
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GetListType();
} private void GetListType()
{
DropDownListMenu.Items.Clear();
DropDownListMenu.DataSource = ListTypeForEnum();
DropDownListMenu.DataValueField = "Value";
DropDownListMenu.DataTextField = "Text";
DropDownListMenu.DataBind();
}
public static IList ListTypeForEnum()
{
ArrayList list = new ArrayList();
foreach (int i in Enum.GetValues(typeof(EnumActivityType)))
{
ListItem listitem = new ListItem(Enum.GetName(typeof(EnumActivityType), i), i.ToString());
list.Add(listitem);
}
return list;
}
public enum EnumActivityType
{
[Description("测试1")]//将枚举值直接转为中文
Task = ,
Fax = ,
Phone = ,
Email = ,
Reminder = ,
Appointment = ,
Expiration = ,
Inquiry =
}
}