实例介绍asp.net项目开发中枚举的使用


本文摘自PHP中文网,作者伊谢尔伦,侵删。

这篇文章主要介绍介绍asp.net项目开发中枚举的使用。包含了显示枚举的值和为下拉框绑定枚举两个功能举例说明。

1 显示枚举的值:<%# (CN80s.DDPM.Model.Enum.EnumBidCardStatus)(int)Eval("PerpaidCard_Status")%>
2 为下拉框绑定枚举:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

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项目开发中枚举的使用的详细内容!

相关阅读 >>

.net项目中上传大图片失败

asp.net禁用页面缓存的方法总结

关于asp.net视频教程的资源分享

asp.net core 发送邮件

asp.net实现群发邮件功能实例教程

学习asp.net core 2遇到的问题分享

asp.net core实例教程之配置

asp.net core中间件设置教程(7)_实用技巧

asp.net数据库密码:md5加密算法详解

详解asp.net使用ajax实现分页局部刷新页面功能的代码实例

更多相关阅读请进入《asp.net》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...