Asp.Net 将枚举类型(enum)绑定到ListControl(DropDownList)控件
2016-11-27 00:30
681 查看
在开发过程中一些状态的表示使用到枚举类型,那么如何将枚举类型直接绑定到ListControl(DropDownList)是本次的主题,废话不多说了,直接代码:
首先看工具类代码:
工具类的使用 新建一个WebForm1.aspx页面:
效果:
首先看工具类代码:
/// <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")); } } } }
效果:
相关文章推荐
- 绑定Enum到ASP.NET数据绑定控件的完美解决方案[05/26修订]——增加支持第三方枚举描述,支持二进制与过的枚举值
- asp.net IList查询数据后格式化数据再绑定控件
- ASP.NET中DropDownList控件的数据绑定的C#语言描述
- 在.Net中将Enum绑定到ComboBox控件 (思想适用于Asp.net和WinForm)
- Asp.net Webform 数据源绑定控件的扩展(懒人的办法):DropDownList
- 教学思路ASP.Net之服务器控件:三、DropDownList、ListBox、CheckBoxList、RadioButtonList控件
- ASP.NET服务器端控件RadioButtonList,DropDownList,CheckBoxList的取值、赋值用法
- ASP.NET服务器端控件RadioButtonList,DropDownList,CheckBoxList的取值、赋值用法
- asp.net2.0中Gridview控件中如何绑定dropdownlist控件-.NET技术/ASP.NET
- ASP.NET DEMO Ⅳ : 使用数据源控件将数据绑定到 ListControl 上
- asp.net mvc 将Enum绑定在DropDownList了
- asp.net ajaxtoolkit CascadingDropDown从数据库中读取数据绑定到DropDownList控件上
- ASP.NET中DropDownList控件的数据绑定的C#语言描述
- ASP.NET Repeater控件绑定List泛型的写法
- asp.net IList查询数据后格式化数据再绑定控件
- asp.net学习之 数据绑定控件--List数据绑定控件
- ASP.NET中 RadioButtonList(单选按钮组),CheckBoxList(复选框组),DropDownList(下拉框),ListBox(列表框),BulletedList(无序列表)的使用前台绑定
- 教学思路ASP.Net之服务器控件:三、DropDownList、ListBox、CheckBoxList、RadioButtonList控件
- ASP.NET的控件变量类型:ListItem、DropDownList
- asp.net mvc 将Enum绑定在DropDownList了