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

相关阅读 >>

分享asp.net学习笔记(6)webpages 表单

在iis上部署asp.net core项目的步骤

详细介绍一款.net代码编辑控件(icsharpcode.texteditor)

asp.net core项目配置教程(6)_实用技巧

使用action的模型绑定实例教程

asp.net core 返回 json datetime 格式

常用的asp.net 技巧总结

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

asp.net core环境设置教程(2)_实用技巧

asp.net教程--mvc中signalr的基础讲解

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




打赏

取消

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

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

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

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

评论

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