【项目分析】通用的枚举类型的下拉列表绑定(3)

发现项目中很多采用了硬编码的方式,去绑定下拉框数据,用法相当地挫。之前没有去写个程序梳理下,于是,稍微实现了下通用的绑定方式。

有这样一个学科枚举类型:

复制代码
代码
/// <summary> 
/// 学科 
/// </summary> 
public enum Subject 

     None 
= 0
     [Description(
"语文")] 
     Chinese 
= 1

     [Description(
"数学")] 
     Mathematics 
= 2

     [Description(
"英语")] 
     English 
= 3

     [Description(
"政治")] 
     Politics 
= 4

     [Description(
"物理")] 
     Physics 
= 5

     [Description(
"化学")] 
     Chemistry 
= 6

     [Description(
"历史")] 
     History 
= 7

     [Description(
"地理")] 
     Geography 
= 8

     [Description(
"生物")] 
     Biology 
= 9 
}
复制代码

这里使用了一个Description特性,目的是为了在一个DropDownList列表中绑定文本数据。

这里使用了一个扩展方法,目的为了返回一个Value和Text的数据列表,上方法:

 

复制代码
代码
    /// <summary>
    
/// 枚举辅助类
    
/// </summary>
    public static class EnumHelper
    {
        
/// <summary> 
        
/// 获得枚举类型数据项(不包括空项)
        
/// </summary> 
        
/// <param name="enumType">枚举类型</param> 
        
/// <returns></returns> 
        public static IList<object> GetItems(this Type enumType)
        {
            
if (!enumType.IsEnum)
                
throw new InvalidOperationException();

            IList
<object> list = new List<object>();

            
// 获取Description特性 
            Type typeDescription = typeof(DescriptionAttribute);
            
// 获取枚举字段
            FieldInfo[] fields = enumType.GetFields();
            
foreach (FieldInfo field in fields)
            {
                
if (!field.FieldType.IsEnum)
                    
continue;

                 
// 获取枚举值
                int value = (int)enumType.InvokeMember(field.Name, BindingFlags.GetField, nullnullnull);

                
// 不包括空项
                if (value > 0)
                {
                    
string text = string.Empty;
                    
object[] array = field.GetCustomAttributes(typeDescription, false);

                    
if (array.Length > 0) text = ((DescriptionAttribute)array[0]).Description;
                    
else text = field.Name; //没有描述,直接取值

                    
//添加到列表
                    list.Add(new { Value = value, Text = text });
                }
            }
            
return list;
        }
    }
复制代码

 

这里采用特性反射的方式得到了对应的Value和Text,最后返回了一个new { Value = …, Text = … }的匿名类的列表。

那么页面上实现就相当简单了:

复制代码
代码
<asp:DropDownList ID="ddlSubject" runat="server"></asp:DropDownList>

ddlSubject.DataSource 
= typeof(Subject).GetItems(); 
ddlSubject.DataTextField 
= "Text"
ddlSubject.DataValueField 
= "Value"
ddlSubject.DataBind();
复制代码

 

最后得到一个绑定学科的下拉列表:

1

这,非常简单:)

 

PS:全国资源最全最有效的高中教育学习网站:http://edu.591up.com/

posted @   Leepy  阅读(3022)  评论(5编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
历史上的今天:
2009-08-15 JS版Stopwatch计时器
点击右上角即可分享
微信分享提示