
<asp:DropDownList ID="DDLType" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DDLType_SelectedIndexChanged" style="width:135px;"></asp:DropDownList>
1.绑定数据
(1)方法一:
//药柜类型
foreach (ChestType co in Enum.GetValues(typeof(ChestType)))
{
string _val = co.ToString();
string _v = ((Int32)Enum.Parse(typeof(ChestType), _val)).ToString();
string _key = GetEnumDesc(typeof(ChestType), co);
DDLType.Items.Add(new ListItem(_key, _v));
}
///
/// 获得Enum类型description
/// 创建人:Porschev
/// 创建时间:2011-7-19
///
///枚举的类型
///枚举值
///string
public static string GetEnumDesc(Type enumType, object val)
{
string enumvalue = System.Enum.GetName(enumType, val);
if (string.IsNullOrEmpty(enumvalue))
{
return "";
}
System.Reflection.FieldInfo finfo = enumType.GetField(enumvalue);
object[] enumAttr = finfo.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), true);
if (enumAttr.Length > 0)
{
System.ComponentModel.DescriptionAttribute desc = enumAttr[0] as System.ComponentModel.DescriptionAttribute;
if (desc != null)
{
return desc.Description;
}
}
return enumvalue;
}
(2)方法二:
BindEnumList(DDLState, typeof(ItemState), new ListItem("请选择", "-1"));
DDLState.SelectedValue = "1";
/// <summary>
/// Mr.Tom 下拉列表框绑定枚举
/// </summary>
/// <param name="droplist">DropDownList名称</param>
/// <param name="enumType">要绑定的枚举类型</param>
/// <param name="li">第一项要显示的.eg:--请选择--</param>
/// <returns></returns>
public static void BindEnumList(DropDownList droplist, Type enumType, ListItem li)
{
droplist.Items.Clear();
if (enumType.IsEnum == false)
{
return;
}
droplist.Items.Add(li);
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;
}
droplist.Items.Add(new ListItem(strText, strValue));
}
}
2.获取数据
DDLType.SelectedValue = chestInfo.ChestType.GetHashCode().ToString();
声明:此文系舞林cuzn(www.wulinlw.org)原创稿件,转载请保留版权