Home  >  Article  >  Backend Development  >  Examples introducing the use of enumerations in asp.net project development

Examples introducing the use of enumerations in asp.net project development

伊谢尔伦
伊谢尔伦Original
2017-04-29 14:26:251457browse

This article mainly introduces the use of enumerations in asp.net project development. Contains examples of two functions: displaying enumeration values ​​and binding enumerations to drop-down boxes.

1 Display the value of the enumeration: 618c63e41502f5a679dadaa58e424651
2 Bind the enumeration to the drop-down box Example:

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; 
}

The above is the detailed content of Examples introducing the use of enumerations in asp.net project development. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn