通用 Asp.Net DropDownList 绑定枚举类型 实例
2010-12-29 22:07
405 查看
/// <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 下拉列表框绑定枚举
/// </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));
}
}
相关文章推荐
- Asp.Net 将枚举类型(enum)绑定到ListControl(DropDownList)控件
- asp.net的DropDownList通用绑定代码
- ASP.NET MVC- DropDownList绑定
- Asp.Net 之 枚举类型的下拉列表绑定
- asp.net实现XML文件读取数据绑定到DropDownList的方法
- asp.net DropDownList绑定树形数据源
- ASP.Net DropDownList绑定树状结构
- ASP.NET - List<> 绑定 DropDownList
- ASP.NET dropdownlist绑定数据源两种方法
- asp.net 获取安装的所有字体,并绑定dropdownlist
- asp.net: DropDownList绑定数据源及选中某个值的公用方法及例子
- ASP.NET中 DropDownList+Repeater的使用前台绑定
- ASP.NET中DropDownList控件的数据绑定的C#语言描述
- asp.net+JSON+Ajax(基于Prototype框架1.4)做无刷新的2级DropDownList代码实例
- [ASP.NET]利用DataTable对DropDownList进行数据绑定
- Asp.net Webform 数据源绑定控件的扩展(懒人的办法):DropDownList
- Asp.Net MVC绑定DropDownList等控件
- 绑定access数据库,asp.net 中 以DropDownList为查询条件 ,前台实现数据查询
- (asp.net c#)DropDownList绑定后显示对应的项的两种方法