扩展可以支持枚举类型的DorpDownList控件
我们有很多时候,都会定义一些枚举类型的变量,而我们用的时候,也习惯用DropDownList绑定,绑定的方式也有好多种,比如集合,泛型集合等等。
比如:我们定义一个图片后缀名的枚举类型
public enum PicType
{
Jpg = 0,
Gif =1,
Png =2
}
如果我们想用这个枚举类型来作为DropDownList的数据源的话,我们可以这么做:{
Jpg = 0,
Gif =1,
Png =2
}
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
ddlEnumTypeBind();
}
}
private void ddlEnumTypeBind()
{
this.ddlenumtype.DataSource = Enum.GetNames(typeof(PicType));
this.ddlenumtype.DataBind();
}
但是用以上代码,DropDownList呈现的DataTextField和DataValueField都是枚举类型的字符表示也就是(Jpg,Gif..),得到它的Value ,我们可以这样:
{
if(!Page.IsPostBack)
{
ddlEnumTypeBind();
}
}
private void ddlEnumTypeBind()
{
this.ddlenumtype.DataSource = Enum.GetNames(typeof(PicType));
this.ddlenumtype.DataBind();
}





当然,如果我们想要得到枚举类型的数字值,我们可以用泛型(保证类型安全),集合等等。
泛型Code:
public static SortedList<string, int> GetEnumDataSource<T>() where T : struct
{
Type myEnumType = typeof(T);
if (myEnumType.BaseType != typeof(Enum))
{
throw new ArgumentException("Type T must inherit from System.Enum.");
}
SortedList<string, int> returnCollection = new SortedList<string, int>();
string[] enumNames = Enum.GetNames(myEnumType);
for (int i = 0; i < enumNames.Length; i++)
{
returnCollection.Add(enumNames[i],
(int)Enum.Parse(myEnumType, enumNames[i]));
}
return returnCollection;
}
{
Type myEnumType = typeof(T);
if (myEnumType.BaseType != typeof(Enum))
{
throw new ArgumentException("Type T must inherit from System.Enum.");
}
SortedList<string, int> returnCollection = new SortedList<string, int>();
string[] enumNames = Enum.GetNames(myEnumType);
for (int i = 0; i < enumNames.Length; i++)
{
returnCollection.Add(enumNames[i],
(int)Enum.Parse(myEnumType, enumNames[i]));
}
return returnCollection;
}
集合Code:
public static IList GetEnumTypeList(Type type)
{
ArrayList aList = new ArrayList();
foreach (int i in Enum.GetValues(type))
{
ListItem listItem = new ListItem(Enum.GetName(type, i), i.ToString());
aList.Add(listItem);
}
return aList;
}
{
ArrayList aList = new ArrayList();
foreach (int i in Enum.GetValues(type))
{
ListItem listItem = new ListItem(Enum.GetName(type, i), i.ToString());
aList.Add(listItem);
}
return aList;
}
在这里我只说明用泛型作为DropDownList的数据源,集合类似
private void ddlEnumTypeBind()
{
this.ddlenumtype.DataSource = GetEnumDataSource<PicType>();
this.ddlenumtype.DataTextField = "Key";
this.ddlenumtype.DataValueField = "Value";
this.ddlenumtype.DataBind();
}
{
this.ddlenumtype.DataSource = GetEnumDataSource<PicType>();
this.ddlenumtype.DataTextField = "Key";
this.ddlenumtype.DataValueField = "Value";
this.ddlenumtype.DataBind();
}
现在,我们写了一系列的代码,说明怎样用枚举类型作为DropDownlist的数据源,但是我们可以发现如果一个页面要用到许多这样的代码,或者多个页面也要用到这样的代码,那么我们的代码是不是有好多的冗余呢,又怎么管理呢?所以我们可以把这个封装到控件中,扩展一下DropDownList,使其支持枚举类型。
Code like the following:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.ComponentModel;
namespace Xbf.Common
{
/// <summary>
/// EnumDorpDownList 的摘要说明
/// </summary>
public class EnumDorpDownListControl<T> : System.Web.UI.WebControls.DropDownList where T : struct
{
public EnumDorpDownListControl()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 是否显示全部选项
/// </summary>
private bool _showAllOption = false;
public bool ShowAllOption
{
get
{
return _showAllOption;
}
set
{
_showAllOption = value;
}
}
public new T? SelectedValue
{
get
{
if (String.IsNullOrEmpty(this.SelectedItem.Text))
{
return null;
}
return (T)Enum.Parse(typeof(T), this.SelectedItem.Text);
}
}
public int? SelectedIntegerValue
{
get
{
if (ShowAllOption && this.SelectedIndex == 0)
{
return null;
}
if (String.IsNullOrEmpty(this.SelectedItem.Text))
{
return null;
}
return (int)Enum.Parse(typeof(T), this.SelectedValue.ToString());
}
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (typeof(T).BaseType != typeof(Enum))
{
throw new ArgumentException("Type T must inherit from System.Enum.");
}
BindData();
}
/// <summary>
/// 数据绑定
/// </summary>
protected void BindData()
{
this.DataSource = GetEnumDataSource<T>();
this.DataTextField = "Key";
this.DataValueField = "Value";
this.DataBind();
if (ShowAllOption)
{
this.Items.Insert(0, new ListItem("All", ""));
}
}
public static SortedList<string, int> GetEnumDataSource<T>() where T : new()
{
Type myEnumType = typeof(T);
if (myEnumType.BaseType != typeof(Enum))
{
throw new ArgumentException("Type T must inherit from System.Enum.");
}
SortedList<string, int> returnCollection = new SortedList<string, int>();
string[] enumNames = Enum.GetNames(myEnumType);
for (int i = 0; i < enumNames.Length; i++)
{
returnCollection.Add(enumNames[i],
(int)Enum.Parse(myEnumType, enumNames[i]));
}
return returnCollection;
}
}
}
页面调用代码:using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.ComponentModel;
namespace Xbf.Common
{
/// <summary>
/// EnumDorpDownList 的摘要说明
/// </summary>
public class EnumDorpDownListControl<T> : System.Web.UI.WebControls.DropDownList where T : struct
{
public EnumDorpDownListControl()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 是否显示全部选项
/// </summary>
private bool _showAllOption = false;
public bool ShowAllOption
{
get
{
return _showAllOption;
}
set
{
_showAllOption = value;
}
}
public new T? SelectedValue
{
get
{
if (String.IsNullOrEmpty(this.SelectedItem.Text))
{
return null;
}
return (T)Enum.Parse(typeof(T), this.SelectedItem.Text);
}
}
public int? SelectedIntegerValue
{
get
{
if (ShowAllOption && this.SelectedIndex == 0)
{
return null;
}
if (String.IsNullOrEmpty(this.SelectedItem.Text))
{
return null;
}
return (int)Enum.Parse(typeof(T), this.SelectedValue.ToString());
}
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (typeof(T).BaseType != typeof(Enum))
{
throw new ArgumentException("Type T must inherit from System.Enum.");
}
BindData();
}
/// <summary>
/// 数据绑定
/// </summary>
protected void BindData()
{
this.DataSource = GetEnumDataSource<T>();
this.DataTextField = "Key";
this.DataValueField = "Value";
this.DataBind();
if (ShowAllOption)
{
this.Items.Insert(0, new ListItem("All", ""));
}
}
public static SortedList<string, int> GetEnumDataSource<T>() where T : new()
{
Type myEnumType = typeof(T);
if (myEnumType.BaseType != typeof(Enum))
{
throw new ArgumentException("Type T must inherit from System.Enum.");
}
SortedList<string, int> returnCollection = new SortedList<string, int>();
string[] enumNames = Enum.GetNames(myEnumType);
for (int i = 0; i < enumNames.Length; i++)
{
returnCollection.Add(enumNames[i],
(int)Enum.Parse(myEnumType, enumNames[i]));
}
return returnCollection;
}
}
}
protected EnumDorpDownListControl<PicType> ProposalStatusDropDownList1;
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
ProposalStatusDropDownList1 = new EnumDorpDownListControl<PicType>();
this.PlaceHolder1.Controls.Add(ProposalStatusDropDownList1);
}
在这里,DropDownList被当作PlaceHolder的子控件显示在页面上,如果要取得其中的值可以这样:protected override void OnInit(EventArgs e)
{
base.OnInit(e);
ProposalStatusDropDownList1 = new EnumDorpDownListControl<PicType>();
this.PlaceHolder1.Controls.Add(ProposalStatusDropDownList1);
}
protected void Button1_Click(object sender, EventArgs e)
{
this.Label1.Text = ProposalStatusDropDownList1.SelectedIntegerValue.ToString();
}
{
this.Label1.Text = ProposalStatusDropDownList1.SelectedIntegerValue.ToString();
}
是不是比先前的那个好多了,但是还有一个不方便之处就是不支持拖放,因为自己才疏学浅,没有能把这个改造成可以拖放的,所以在此如果哪位能帮忙改造一下请在以下贴出或留下链接地址,在此先谢了。
文章出处:http://aspalliance.com/1514_Extending_the_DropDownList_to_Support_Enums.all