Tips used in the development of the asp.net project

  • 2020-05-09 18:25:05
  • OfStack

1 display the values of the enumeration: < %# (CN80s.DDPM.Model.Enum.EnumBidCardStatus)(int)Eval("PerpaidCard_Status")% >
2 binding enumeration for the drop-down box:
 
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("-- all --", "")); 
} 
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; 
} 

Related articles: