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"));
            }
        }
    }
}

效果:

转载于:https://www.cnblogs.com/linJie1930906722/p/6105789.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值