实例介绍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使用ajax实现分页局部刷新页面功能的代码实例

如何在asp.net core中使用cookie中间件的详细介绍

asp.net性能监控及其优化入门

asp.net core实例详解一

asp.net core应用中与第三方ioc/di框架的整合

分享一个“网红脸“”框架实例教程

c#中list的用法

.net存储pdf、word和excel到数据库的方法详解

asp.net mvc5请求处理管道和生命周期的详细教程

asp.net(c#)如何读取excel的文件的实例详解

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




打赏

取消

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

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

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

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

评论

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