实现:根据定义的枚举,展示下拉框
解决:
1、 页面
<td style="white-space: nowrap" align="right"> 单位等级:<asp:DropDownList ID="ddle_DeptLevel_i" runat="server" width="121px"></asp:DropDownList></td>
2、数据库中的定义
3、赋值
{//单位等级,普通=0,重要=1,非常重要=2 Dictionary<int,string> dicall = ITEM.Inherits.Bll.CollEnum_Mis4.e_DeptLevel.GetDicAll(); this.ddle_DeptLevel_i.Items.Clear(); foreach(int ikey in dicall.Keys) { this.ddle_DeptLevel_i.Items.Add(new ListItem(dicall[ikey].ToString(),ikey.ToString())); } this.ddle_DeptLevel_i.Items.Insert(0,new ListItem()); }
枚举中的 GetDicAll() 方法
public static Dictionary<int, string> GetDicAll() { Dictionary<int, string> dic = new Dictionary<int, string>(); dic.Add((int)Enum.普通, Enum.普通.ToString()); dic.Add((int)Enum.重要, Enum.重要.ToString()); dic.Add((int)Enum.非常重要, Enum.非常重要.ToString()); return dic; }
枚举定义
#region e_DeptLevel,单位等级,普通=0,重要=1,非常重要=2 /// <summary> /// e_DeptLevel,单位等级,普通=0,重要=1,非常重要=2 /// </summary> public struct e_DeptLevel { /// <summary> ///e_DeptLevel,单位等级,普通=0,重要=1,非常重要=2 /// </summary> public enum Enum { 普通 = 0, 重要 = 1, 非常重要 = 2, } /// <summary> /// 获取整数集合 /// </summary> /// <returns></returns> public static string GetIntAll() { string s = string.Empty; Dictionary<int, string> dic = GetDicAll(); foreach (int key in dic.Keys) { string sign = s == string.Empty ? "" : ","; s += sign + key.ToString(); } return s; } /// <summary> /// 获取字符集合 /// </summary> /// <returns></returns> public static string GetStringAll() { string s = string.Empty; Dictionary<int, string> dic = GetDicAll(); foreach (int key in dic.Keys) { string sign = s == string.Empty ? "" : ","; s += sign + dic[key].ToString(); } return s; } /// <summary> /// 获取所有的枚举类型 /// </summary> /// <returns></returns> public static Dictionary<int, string> GetDicAll() { Dictionary<int, string> dic = new Dictionary<int, string>(); dic.Add((int)Enum.普通, Enum.普通.ToString()); dic.Add((int)Enum.重要, Enum.重要.ToString()); dic.Add((int)Enum.非常重要, Enum.非常重要.ToString()); return dic; } /// <summary> /// 枚举转化 /// </summary> public static string GetStringByEnum(Enum e) { return e.ToString(); } /// <summary> /// 枚举转化 /// </summary> public static string GetStringByInt(int i) { if (i == (int)Enum.普通) return Enum.普通.ToString(); else if (i == (int)Enum.重要) return Enum.重要.ToString(); else if (i == (int)Enum.非常重要) return Enum.非常重要.ToString(); else { Exception ex = new Exception("Enum 转化错误"); throw ex; } } /// <summary> /// 枚举转化 /// </summary> public static Enum GetEnumByString(string s) { if (s == Enum.普通.ToString()) return Enum.普通; else if (s == Enum.重要.ToString()) return Enum.重要; else if (s == Enum.非常重要.ToString()) return Enum.非常重要; else { Exception ex = new Exception("Enum 转化错误"); throw ex; } } /// <summary> /// 枚举转化 /// </summary> public static Enum GetEnumByString(int i) { if (i == (int)Enum.普通) return Enum.普通; else if (i == (int)Enum.重要) return Enum.重要; else if (i == (int)Enum.非常重要) return Enum.非常重要; else { Exception ex = new Exception("Enum 转化错误"); throw ex; } } /// <summary> /// 枚举转化 /// </summary> public static int GetIntByEnum(Enum e) { return (int)e; } /// <summary> /// 枚举转化 /// </summary> public static int GetIntByString(string s) { Enum e = GetEnumByString(s); return GetIntByEnum(e); } } #endregion