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
  • 相关阅读:
    四则运算2
    大二第二学期阅读计划
    第一周课堂练习
    《人月神话》读后感
    软件工程概论总结第十一章
    软件工程概论总结第十章
    软件工程概论总结第九章
    软件工程概论总结第八章
    软件工程概论总结第七章
    第五章、软件过程中的形式化方法
  • 原文地址:https://www.cnblogs.com/mrtom/p/1921146.html
Copyright © 2011-2022 走看看