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
  • 相关阅读:
    阿里规范
    阿里规范
    阿里规范
    sql 优化步骤
    事务的并发问题:脏读、幻读和不可重复读
    Hive 常见面试题(二)
    yield 的使用
    Java 线程状态
    Lambda 表达式推演全过程
    IDEA 代码自动补全/自动联想 功能
  • 原文地址:https://www.cnblogs.com/mrtom/p/1921146.html
Copyright © 2011-2022 走看看