C # bind all the descriptions and values in an enumeration to the drop-down list,
/// <Summary>
/// Obtain the description of the enumerated value. If there is no description, the enumerated name is returned.
/// </Summary>
/// <Param name = "en"> enumeration value </param>
/// <Returns> description </returns>
Public static string GetEnumDescription (Enum en)
{
FieldInfo fi = en. GetType (). GetField (en. ToString ());
Object [] attrs = fi. GetCustomAttributes (typeof (DescriptionAttribute), false );
If (attrs! = Null & attrs. Length> 0)
{
Return (DescriptionAttribute) attrs [0]). Description;
}
Else
{
Return en. ToString ();
}
}
ArrayList listcode = new ArrayList (); // defines a set.
Foreach (int I in Enum. GetValues (typeof (ErrorCode) // Add the enumeration description and value loop to the Set (ErrorCode: enumeration name)
{
ListItem listitem = new ListItem (GetEnumDescription (ErrorCode) I), I. ToString ());
// ListItem listitem = new ListItem (Enum. GetName (typeof (EnumActivityType), I), I. ToString (); // obtain the enumeration name set
Listcode. Add (listitem );
}
Certificate -----------------------------------------------------------------------------------------------------------------------------------------------------
For (int I = 0; I <listcode. Count; I ++) // You can output the description and value of each enumeration in a loop.
{
ListItem item = (ListItem) listcode [I];
Var d = item. Text;
Var B = item. Value;
}