这篇文章主要介绍介绍asp.net项目开发中枚举的使用。包含了显示枚举的值和为下拉框绑定枚举两个功能举例说明。
1 显示枚举的值:
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<ListItem> GetEnumList(Type enumType, bool allAllOption) { if (enumType.IsEnum == false) { return null; } List<ListItem> list = new List<ListItem>(); 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; }
以上就是实例介绍asp.net项目开发中枚举的使用的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号