如何把枚举类型绑定DropDownList控件

我们经常会用到枚举类型,而且当作某种类别使用,给用户选择其中一项,那么如何实现枚举当数据使用呢?这里我们不用数据库来存储是因为这个数据是固定的,不需要更改,而且便于统一,我们使用了枚举数据。

下面我们来看我是如何实现的,这里我用的是。net的控件开发,如果你用其他的方式,可以当作参考。

首先 来看我定义了一个枚举类型:

/// <summary>
    /// 类别
    /// </summary>
    public enum JFAccountLDTypeEnum : int
    {
        [Description("政治性")]
        LZhengZhi = 1,
         [Description("学习性")]
        LXueXi = 5,
         [Description("服务性")]
        LFuWu = 10,
         [Description("创新性")]
        LChuangXin = 15,
         [Description("履职性")]
        LLvZhi = 20

    }

我们这里还定义了描述属性,用于下拉框进行显示。

再来我们创建一个控件,。net开发有意思的是,你可以创建各种有趣的控件组合实现某一功能,当然需要对控件比较了解。

这里我们只用一个下拉框控件就可以了。看设计页面代码:

<asp:dropdownlist id="ddlLDType" runat="server"></asp:dropdownlist>

 命名为,SelectLDTypeControl.ascx 下面是后台代码:

 1 private int selectedLDTypeId;
 2 
 3         public void BindData()
 4         {
 5             ddlLDType.Items.Clear();
 6             ddlLDType.Items.Add(new ListItem(this.EmptyItemText, "0"));
 7             //获取枚举元素
 8             FieldInfo[] fields = typeof (JFAccountLDTypeEnum).GetFields(BindingFlags.Static | BindingFlags.Public);
 9             foreach (var field in fields)
10             {
11                // string value = ((JFAccountLDTypeEnum)field.GetValue(null)).ToString();
12                //string value = Convert.ToString((JFAccountLDTypeEnum)field.GetValue(null)); //获取列表
13                string value = field.GetRawConstantValue().ToString(); // 获取值
14               //获取描述,利用描述来表达中文选项
15                 var attribute = Attribute.GetCustomAttribute(field,
16                         typeof(DescriptionAttribute)) as DescriptionAttribute;
17                 if (attribute != null)
18                 {
19                     ListItem listitem = new ListItem(attribute.Description, value);
20                     ddlLDType.Items.Add(listitem);
21                     if (value == this.selectedLDTypeId.ToString())
22                         listitem.Selected = true;
23                 }
24             }
25             this.ddlLDType.DataBind();
26         }
27 
28         
29 
30         
31         // 例子
32         public static class EnumEx
33         {
34             public static T GetValueFromDescription<t>(string description)
35             {
36                 var type = typeof(T);
37                 if (!type.IsEnum) throw new InvalidOperationException();
38                 foreach (var field in type.GetFields())
39                 {
40                     var attribute = Attribute.GetCustomAttribute(field,
41                         typeof(DescriptionAttribute)) as DescriptionAttribute;
42                     if (attribute != null)
43                     {
44                         if (attribute.Description == description)
45                             return (T)field.GetValue(null);
46                     }
47                     else
48                     {
49                         if (field.Name == description)
50                             return (T)field.GetValue(null);
51                     }
52                 }
53                 throw new ArgumentException("Not found.", "description");
54                 // or return default(T);
55             }
56         }
57 
58         
59         protected void Page_Load(object sender, EventArgs e)
60         {
61         }
62 
63         public string CssClass
64         {
65             get
66             {
67                 return ddlLDType.CssClass;
68             }
69             set
70             {
71                 ddlLDType.CssClass = value;
72             }
73         }
74 
75         public int SelectedLDTypeId
76         {
77             get
78             {
79                 return int.Parse(this.ddlLDType.SelectedItem.Value);
80             }
81             set
82             {
83                 this.selectedLDTypeId = value;
84             }
85         }
86 
87 
88 
89         public string EmptyItemText
90         {
91             get
92             {
93                 if (ViewState["EmptyItemText"] == null)
94                     return "[ --- ]";
95                 else
96                     return (string)ViewState["EmptyItemText"];
97             }
98             set { ViewState["EmptyItemText"] = value; } </t>
99         }

如何使用? 在aspx 页面直接引用我们做的控件SelectLDTypeControl 就可以了 后台页面绑定下数据。 如下:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        this.ctrlSelectLDType.SelectedLDTypeId = 20;
        this.ctrlSelectLDType.BindData();        
    }

}

这样我们就完成了一个下拉控件绑定 枚举类型数据的自定义控件了,可以在需要的地方任意使用,这是不是很方便。

 

posted @ 2021-08-25 21:53  Amatutu  阅读(49)  评论(0)    收藏  举报