zoukankan      html  css  js  c++  java
  • Databinding enumerators to DataBound controls http://www.codeproject.com/Tips/567522/DatabindingenumeratorstoDataBoundcontrols 武胜

    Introduction

    The objective is binding Enums to any ListControl, actually to any DataBoundControl such as DropdownList, Repeaters, etc, avoiding indexes hard coding and bad practices... In this brief code I provide you kind of quick workaround that enhance the portability and maintainability of the source code. 

    As you will see below, the idea is to have any enum bound, keeping it simple. 

    Enums are basically a set of named constants. They are declared in C# using the  enum keyword. Every enum type automatically derives from System.Enum and thus we can use  System.Enum methods on our Enums.   

    Here, we are defining a Mode enum having two values: Iterative and Secuential

    enum Mode {Iterative, Secuential}

    Using the code 

    First of all, we might define the Mode enum members by using DescriptionAttribute within ComponentModel, and IsDefault custom attribute as it is wanted to be shown in the dropdownlist.

    public enum Mode
    {
        [Description("Iterative option"), IsDefault(true)]
        Iterative,
        [Description("Secuential option"), IsDefault(false)]
        Sequential
    } 

    An ASP.NET list control can be bound to any data source that implements the IList, IEnumerable, ICollection, here we are binding it adding all the items in the array by using AddRange method. 

    The idea is to create an extension method "ToItems" that allows us turn  enum values into ListItem[]

    this.drpMode.Items.AddRange(typeof(Mode).ToItems()); 

    When it is necessary to  get the selected IList value and match it back with the  enum value we can perform it by using another extension method. I think this is valid just in .NET 4.0 but in order to keep the feature in previous versions it's added the extension method within. The entire piece of code can be wrapped it into a control when necessary.

    Mode value = EnumExtentions.Parse<Mode>(this.drpMode.SelectedValue);

    Background

    First of all, I found it neat to create a custom default attribute to be annotated it in order to let the dropdownlist know which is the default selected item. 

    [AttributeUsage(AttributeTargets.All)]
    public class IsDefaultAttribute : Attribute
    {
        public bool IsDefault{ get; set; }
    
        public IsDefaultAttribute(bool isDefault)
        {
    	    IsDefault = isDefault;
    	}
    } 

    Finally, by LINQ we query enum both values and annotations and create  a ListItem collection as below.

    public static ListItem[] ToItems(this Type enumType) {
         var items = from a in enumType.GetFields()
         where !a.IsSpecialName
         select new ListItem()
         {
            Value = a.GetValue(a).ToString(),
            Text = (a.GetCustomAttributes(typeof(DescriptionAttribute), false)[0] 
    	as DescriptionAttribute).Description,
            Selected = (a.GetCustomAttributes(typeof(IsDefaultAttribute), false)[0] 
    	as IsDefaultAttribute).IsDefault};
         return items.ToArray<ListItem>();
    }
  • 相关阅读:
    总结系列_3(opencv中c版本和c++版本区别体验,续...)
    深入理解JavaScript系列(29):设计模式之装饰者模式
    深入理解JavaScript系列(33):设计模式之策略模式
    深入理解JavaScript系列(35):设计模式之迭代器模式
    深入理解JavaScript系列(36):设计模式之中介者模式
    深入理解JavaScript系列(34):设计模式之命令模式
    深入理解JavaScript系列(31):设计模式之代理模式
    深入理解JavaScript系列(37):设计模式之享元模式
    深入理解JavaScript系列(32):设计模式之观察者模式
    大叔手记(21):汤姆大叔博客园开博100天总结
  • 原文地址:https://www.cnblogs.com/zeroone/p/2988411.html
Copyright © 2011-2022 走看看