> 백엔드 개발 > C#.Net 튜토리얼 > asp.net 프로젝트 개발에서 열거형 사용을 소개하는 예

asp.net 프로젝트 개발에서 열거형 사용을 소개하는 예

伊谢尔伦
풀어 주다: 2017-04-29 14:26:25
원래의
1570명이 탐색했습니다.

이 기사에서는 asp.net 프로젝트 개발에서 열거형의 사용을 주로 소개합니다. 열거 값 표시 및 열거를 드롭다운 상자에 바인딩하는 두 가지 기능의 예가 포함되어 있습니다.

1 열거형 값을 표시합니다: <%# (CN80s.DDPM.Model.Enum.EnumBidCardStatus)(int)Eval("PerpaidCard_Status")%> 드롭다운 상자 예:

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 프로젝트 개발에서 열거형 사용을 소개하는 예의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿