Asp.net General enumeration methods such as drop-down list to bind
To do a project, which spent a lot of enumeration. Wrote in a separate drawing page list, so thought that it was very uniform. So you google the enumeration method of binding the drop-down list and found that the method is not universal. Then modified the line of code written in a generic way to enumerate the list binding. Paste the following code.
1 /// <summary>
4 /// </summary>
5 /// <param name=”col”>Control Name</param>
6 /// <param name=”eumeName”>Enum Name</param>
7 public static void BindData(Control col, string enumName)
8 {
9 System.Reflection.Assembly asm = Assembly.Load(“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 }
As enumerated in the project which is written above the words System.Reflection.Assembly asm = Assembly.Load ( “App_Code”); read System.Reflection.Assembly asm = Assembly.Load ( “into-way set”);
Call for PublicFun.BindData ( “Control ID”, “namespace. Enumerated”);
Posted about the way other types of data binding is as follows ….
TABLE binding table from the drop-down cases of the general method table
Code
1 /// <summary>
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 }
Bind Hashtable table drop-down cases of a general method
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 }