Asp.Net 将枚举类型(enum)绑定到ListControl(DropDownList)控件
在开发过程中一些状态的表示使用到枚举类型,那么如何将枚举类型直接绑定到ListControl(DropDownList)是本次的主题,废话不多说了,直接代码:
首先看工具类代码:
/// <summary> /// 通过枚举类型 绑定到ListControl 控件的通用类 /// 用法:直接传入要绑定的Control: EnumManager<枚举>.Bind_Enum_Control(ListControl); /// </summary> /// <typeparam name="TEnum"></typeparam> public static class EnumManager<TEnum> where TEnum : struct, System.IConvertible { private static readonly System.Collections.Generic.Dictionary<string, System.Collections.Generic.Dictionary<int, string>> EnumDictionary = new System.Collections.Generic.Dictionary<string, System.Collections.Generic.Dictionary<int, string>>(); /// <summary> /// 获取枚举 /// </summary> /// <returns></returns> public static System.Collections.Generic.Dictionary<int, string> GetEnumDictionary() { if (EnumDictionary.ContainsKey(typeof(TEnum).FullName)) return EnumDictionary[typeof(TEnum).FullName]; var EnumDict = Add_Enum_To_Diconary(); return EnumDict; } /// <summary> /// 传入控件进行绑定 /// </summary> /// <param name="control"></param> public static void Bind_Enum_Control(System.Web.UI.WebControls.ListControl control) { control.DataSource = GetEnumDictionary(); control.DataTextField = "value"; control.DataValueField = "key"; control.DataBind(); } /// <summary> /// 通过key获取Enum的值 /// </summary> /// <param name="key"></param> /// <returns></returns> public static string GetEnumValue(int key) { if (EnumDictionary.ContainsKey(typeof(TEnum).FullName)) return EnumDictionary[typeof(TEnum).FullName][key]; var EnumDic = Add_Enum_To_Diconary(); return EnumDic[key]; } private static System.Collections.Generic.Dictionary<int, string> Add_Enum_To_Diconary() { System.Type t = typeof(TEnum); string[] _names = System.Enum.GetNames(t); int[] _values = System.Enum.GetValues(t) as int[]; System.Collections.Generic.Dictionary<int, string> EnumDict = new System.Collections.Generic.Dictionary<int, string>(); for (int i = 0; i < _values.Length; i++) { EnumDict.Add(_values[i], _names[i]); } EnumDictionary.Add(t.FullName, EnumDict); return EnumDict; } }
工具类的使用 新建一个WebForm1.aspx页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Test_ToFixed.WebForm1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> 测试将枚举类型数据绑定到下拉列表: <asp:DropDownList runat="server" ID="ddl"/> </div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace Test_ToFixed { public enum Status { 审核失败 = 0, 未审核 = 1, 审核中 = 2, 已审核 = 3 } public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Common.EnumManager<Status>.Bind_Enum_Control(this.ddl); //将枚举类型绑定到DropDownList控件 this.ddl.Items.Insert(0, new ListItem("请选择", "-1")); } } } }
效果: