做了一个项目,其中用了很多枚举。在页面上单独写下拉列表,这样觉得很不统一。所以google了一下枚举绑定下拉列表的方法,发现都不是通用的方法。于是修改了网上的代码写成了一个通用的枚举绑定列表方法。下面贴代码。
Code
1 /**//// <summary>
2 /// 用枚举绑定列表
3 /// 作者:阿松
4 /// </summary>
5 /// <param name="col">控件名</param>
6 /// <param name="eumeName">枚举名</param>
7 public static void BindData(Control col, string enumName)
8 {
9 System.Reflection.Assembly asm = Assembly.Load("App_Code"); //枚举是在App_Code里
10 Type type = asm.GetType(enumName);
11 FieldInfo[] fields = type.GetFields();
12 int count = fields.Length;
13
14 if (col is DropDownList)
15 {
16 DropDownList ddl = (DropDownList)col;
17 ddl.Items.Clear();
18 for (int i = 1; i < count; i++)
19 {
20 FieldInfo field = fields[i];
21 ListItem item = new ListItem(field.Name);
22 ddl.Items.Add(item);
23 }
24 }
25 else if (col is HtmlSelect)
26 {
27 HtmlSelect ddl = (HtmlSelect)col;
28 ddl.Items.Clear();
29 for (int i = 1; i < count; i++)
30 {
31 FieldInfo field = fields[i];
32 ListItem item = new ListItem(field.Name);
33 ddl.Items.Add(item);
34 }
35 }
36 else if (col is CheckBoxList)
37 {
38 CheckBoxList ddl = (CheckBoxList)col;
39 ddl.Items.Clear();
40 for (int i = 1; i < count; i++)
41 {
42 FieldInfo field = fields[i];
43 ListItem item = new ListItem(field.Name);
44 ddl.Items.Add(item);
45 }
46 }
47 else if (col is RadioButtonList)
48 {
49 RadioButtonList ddl = (RadioButtonList)col;
50 ddl.Items.Clear();
51 for (int i = 1; i < count; i++)
52 {
53 FieldInfo field = fields[i];
54 ListItem item = new ListItem(field.Name);
55 ddl.Items.Add(item);
56 }
57 }
58 else if (col is ListBox)
59 {
60 ListBox ddl = (ListBox)col;
61 ddl.Items.Clear();
62 for (int i = 1; i < count; i++)
63 {
64 FieldInfo field = fields[i];
65 ListItem item = new ListItem(field.Name);
66 ddl.Items.Add(item);
67 }
68 }
69 }
1 /**//// <summary>
2 /// 用枚举绑定列表
3 /// 作者:阿松
4 /// </summary>
5 /// <param name="col">控件名</param>
6 /// <param name="eumeName">枚举名</param>
7 public static void BindData(Control col, string enumName)
8 {
9 System.Reflection.Assembly asm = Assembly.Load("App_Code"); //枚举是在App_Code里
10 Type type = asm.GetType(enumName);
11 FieldInfo[] fields = type.GetFields();
12 int count = fields.Length;
13
14 if (col is DropDownList)
15 {
16 DropDownList ddl = (DropDownList)col;
17 ddl.Items.Clear();
18 for (int i = 1; i < count; i++)
19 {
20 FieldInfo field = fields[i];
21 ListItem item = new ListItem(field.Name);
22 ddl.Items.Add(item);
23 }
24 }
25 else if (col is HtmlSelect)
26 {
27 HtmlSelect ddl = (HtmlSelect)col;
28 ddl.Items.Clear();
29 for (int i = 1; i < count; i++)
30 {
31 FieldInfo field = fields[i];
32 ListItem item = new ListItem(field.Name);
33 ddl.Items.Add(item);
34 }
35 }
36 else if (col is CheckBoxList)
37 {
38 CheckBoxList ddl = (CheckBoxList)col;
39 ddl.Items.Clear();
40 for (int i = 1; i < count; i++)
41 {
42 FieldInfo field = fields[i];
43 ListItem item = new ListItem(field.Name);
44 ddl.Items.Add(item);
45 }
46 }
47 else if (col is RadioButtonList)
48 {
49 RadioButtonList ddl = (RadioButtonList)col;
50 ddl.Items.Clear();
51 for (int i = 1; i < count; i++)
52 {
53 FieldInfo field = fields[i];
54 ListItem item = new ListItem(field.Name);
55 ddl.Items.Add(item);
56 }
57 }
58 else if (col is ListBox)
59 {
60 ListBox ddl = (ListBox)col;
61 ddl.Items.Clear();
62 for (int i = 1; i < count; i++)
63 {
64 FieldInfo field = fields[i];
65 ListItem item = new ListItem(field.Name);
66 ddl.Items.Add(item);
67 }
68 }
69 }
如里枚举是写在项目里面的话 上面 System.Reflection.Assembly asm = Assembly.Load("App_Code");改写成 System.Reflection.Assembly asm = Assembly.Load("成程集");
调用时 为 PublicFun.BindData("控件ID","命名空间.枚举");
顺便贴一下其它的类型的数据的绑定方法 如下....
从TABLE表绑定下拉例表的通用方法
Code
1 /**//// <summary>
2 /// 绑定下拉列表
3 ///作者:阿松
4 /// </summary>
5 /// <param name="col">控件名</param>
6 /// <param name="dt">内存表</param>
7 /// <param name="Name">显示字段</param>
8 /// <param name="value">值字段</param>
9 public static void BindData(Control col, DataTable dt, string Name, string Value)
10 {
11 if (col is DropDownList)
12 {
13 DropDownList ddl = (DropDownList)col;
14 ddl.Items.Clear();
15 ddl.DataSource = dt;
16 ddl.DataTextField = Name;
17 ddl.DataValueField = Value;
18 ddl.DataBind();
19 }
20 else if (col is CheckBoxList)
21 {
22 CheckBoxList ddl = (CheckBoxList)col;
23 ddl.Items.Clear();
24 ddl.DataSource = dt;
25 ddl.DataTextField = Name;
26 ddl.DataValueField = Value;
27 ddl.DataBind();
28 }
29 else if (col is RadioButtonList)
30 {
31 RadioButtonList ddl = (RadioButtonList)col;
32 ddl.Items.Clear();
33 ddl.DataSource = dt;
34 ddl.DataTextField = Name;
35 ddl.DataValueField = Value;
36 ddl.DataBind();
37 }
38 else if (col is ListBox)
39 {
40 ListBox ddl = (ListBox)col;
41 ddl.Items.Clear();
42 ddl.DataSource = dt;
43 ddl.DataTextField = Name;
44 ddl.DataValueField = Value;
45 ddl.DataBind();
46 }
47 else if (col is HtmlSelect)
48 {
49 HtmlSelect ddl = (HtmlSelect)col;
50 ddl.Items.Clear();
51 ddl.DataSource = dt;
52 ddl.DataTextField = Name;
53 ddl.DataValueField = Value;
54 ddl.DataBind();
55 }
56 }
1 /**//// <summary>
2 /// 绑定下拉列表
3 ///作者:阿松
4 /// </summary>
5 /// <param name="col">控件名</param>
6 /// <param name="dt">内存表</param>
7 /// <param name="Name">显示字段</param>
8 /// <param name="value">值字段</param>
9 public static void BindData(Control col, DataTable dt, string Name, string Value)
10 {
11 if (col is DropDownList)
12 {
13 DropDownList ddl = (DropDownList)col;
14 ddl.Items.Clear();
15 ddl.DataSource = dt;
16 ddl.DataTextField = Name;
17 ddl.DataValueField = Value;
18 ddl.DataBind();
19 }
20 else if (col is CheckBoxList)
21 {
22 CheckBoxList ddl = (CheckBoxList)col;
23 ddl.Items.Clear();
24 ddl.DataSource = dt;
25 ddl.DataTextField = Name;
26 ddl.DataValueField = Value;
27 ddl.DataBind();
28 }
29 else if (col is RadioButtonList)
30 {
31 RadioButtonList ddl = (RadioButtonList)col;
32 ddl.Items.Clear();
33 ddl.DataSource = dt;
34 ddl.DataTextField = Name;
35 ddl.DataValueField = Value;
36 ddl.DataBind();
37 }
38 else if (col is ListBox)
39 {
40 ListBox ddl = (ListBox)col;
41 ddl.Items.Clear();
42 ddl.DataSource = dt;
43 ddl.DataTextField = Name;
44 ddl.DataValueField = Value;
45 ddl.DataBind();
46 }
47 else if (col is HtmlSelect)
48 {
49 HtmlSelect ddl = (HtmlSelect)col;
50 ddl.Items.Clear();
51 ddl.DataSource = dt;
52 ddl.DataTextField = Name;
53 ddl.DataValueField = Value;
54 ddl.DataBind();
55 }
56 }
哈希表绑定下拉例表的通用方法
Code
1 /**//// <summary>
2 /// 绑定下拉列表
3 ///作者:阿松
4 /// </summary>
5 /// <param name="col">控件名</param>
6 /// <param name="ht">哈希表</param>
7 public static void BindData(Control col, Hashtable ht, string name, string value)
8 {
9 if (col is HtmlSelect)
10 {
11 HtmlSelect ddl = (HtmlSelect)col;
12 ddl.Items.Clear();
13 ddl.DataSource = ht;
14 ddl.DataTextField = name;
15 ddl.DataValueField = value;
16 ddl.DataBind();
17 }
18 else if (col is CheckBoxList)
19 {
20 CheckBoxList ddl = (CheckBoxList)col;
21 ddl.Items.Clear();
22 ddl.DataSource = ht;
23 ddl.DataTextField = name;
24 ddl.DataValueField = value;
25 ddl.DataBind();
26 }
27 else if (col is RadioButtonList)
28 {
29 RadioButtonList ddl = (RadioButtonList)col;
30 ddl.Items.Clear();
31 ddl.DataSource = ht;
32 ddl.DataTextField = name;
33 ddl.DataValueField = value;
34 ddl.DataBind();
35 }
36 else if (col is ListBox)
37 {
38 ListBox ddl = (ListBox)col;
39 ddl.Items.Clear();
40 ddl.DataSource = ht;
41 ddl.DataTextField = name;
42 ddl.DataValueField = value;
43 ddl.DataBind();
44 }
45 else if (col is DropDownList)
46 {
47 DropDownList ddl = (DropDownList)col;
48 ddl.Items.Clear();
49 ddl.DataSource = ht;
50 ddl.DataTextField = name;
51 ddl.DataValueField = value;
52 ddl.DataBind();
53 }
54 }
1 /**//// <summary>
2 /// 绑定下拉列表
3 ///作者:阿松
4 /// </summary>
5 /// <param name="col">控件名</param>
6 /// <param name="ht">哈希表</param>
7 public static void BindData(Control col, Hashtable ht, string name, string value)
8 {
9 if (col is HtmlSelect)
10 {
11 HtmlSelect ddl = (HtmlSelect)col;
12 ddl.Items.Clear();
13 ddl.DataSource = ht;
14 ddl.DataTextField = name;
15 ddl.DataValueField = value;
16 ddl.DataBind();
17 }
18 else if (col is CheckBoxList)
19 {
20 CheckBoxList ddl = (CheckBoxList)col;
21 ddl.Items.Clear();
22 ddl.DataSource = ht;
23 ddl.DataTextField = name;
24 ddl.DataValueField = value;
25 ddl.DataBind();
26 }
27 else if (col is RadioButtonList)
28 {
29 RadioButtonList ddl = (RadioButtonList)col;
30 ddl.Items.Clear();
31 ddl.DataSource = ht;
32 ddl.DataTextField = name;
33 ddl.DataValueField = value;
34 ddl.DataBind();
35 }
36 else if (col is ListBox)
37 {
38 ListBox ddl = (ListBox)col;
39 ddl.Items.Clear();
40 ddl.DataSource = ht;
41 ddl.DataTextField = name;
42 ddl.DataValueField = value;
43 ddl.DataBind();
44 }
45 else if (col is DropDownList)
46 {
47 DropDownList ddl = (DropDownList)col;
48 ddl.Items.Clear();
49 ddl.DataSource = ht;
50 ddl.DataTextField = name;
51 ddl.DataValueField = value;
52 ddl.DataBind();
53 }
54 }