zoukankan      html  css  js  c++  java
  • 通用 Asp.Net DropDownList 绑定枚举类型 实例

    /// <summary>
    /// Mr.Tom 下拉列表框绑定枚举
    /// </summary>
    /// <param name="droplist">DropDownList名称</param>
    /// <param name="enumType">要绑定的枚举类型</param>

    /// <param name="li">第一项要显示的.eg:--请选择--</param>
    /// <returns></returns>
    public static void bindEnumList(DropDownList droplist, Type enumType, ListItem li)
    {
        droplist.Items.Clear();
        if (enumType.IsEnum == false)
       {
            return;
        }
        droplist.Items.Add(li);

       Type typeDescription = typeof(DescriptionAttribute);
       System.Reflection.FieldInfo[] fields = enumType.GetFields();
       string strText = string.Empty;
       string strValue = string.Empty;
       foreach (FieldInfo field in fields)
       {
         if (field.IsSpecialName) continue;
         strValue = field.GetRawConstantValue().ToString();
         object[] arr = field.GetCustomAttributes(typeDescription, true);
         if (arr.Length > 0)
        {
           strText = (arr[0] as DescriptionAttribute).Description;
        }
        else
        {
           strText = field.Name;
        }

        droplist.Items.Add(new ListItem(strText, strValue));
      }

    }

    Mr.Tom@live.cn
  • 相关阅读:
    Android 逐帧动画
    MAP getLastKnownLocation()返回null的解决
    大数取余
    (a^b)%c和(a/b)%c
    HDU1046 Gridland
    顺序入栈的出栈方法种数
    HDU1021 Fibonacci Again
    HDU1019 Least Common Multiple
    HDU1018 Big Number
    HDU1013 Digital Roots
  • 原文地址:https://www.cnblogs.com/mrtom/p/1921146.html
Copyright © 2011-2022 走看看