[Asp. Net] Four methods to bind data to Dropdownlist,
First, bind the Array to dropdownlist
Program code string [] Month = new string [7] {"January", "February", "March", "0000l", "May", "June ", "July "};
This. DropDownList1.DataSource = Month;
This. DropDownList1.DataBind ();
The first method can only bind a group of data to dropdownlist, because drawdonwlist can bind two types of data. 1 is DataTextField.
2 is DataValueField. Therefore, after the first method is bound, the value of DataTextField = the value of DataTextField
Second, bind the Array to the dropdownlist
Program code ArrayList ar = new ArrayList ();
For (int I = 1; I <= 12; I ++)
{
Ar. Add (I + "month ");
}
This. DropDownList2.DataSource = ar;
This. DropDownList2.DataBind ();
Intuitive writing.
Program code
ArrayList ar = new ArrayList ();
Ar. Add ("January ");
Ar. Add ("February ");
Ar. Add ("March ");
Ar. Add ("April ");
........................................ ..........
This. DropDownList2.DataSource = ar;
This. DropDownList2.DataBind ();
The advantage of the 2nd method is that the ArrayList. the Add method can be used to dynamically Add elements. For example, if there is a DataTable, We need to read the data in the DataTable and Add it to the Arraylist.
View the code shown below
Program code ArrayList ar = new ArrayList ();
DataTable dt = dataset. Tables [0]
Foreach (DataRow dr in dt. Rows)
{
Ar. Add (dr [0]. ToString ());
}
The code above reads the value of the first grid in a row of data in the Table cyclically from a DataTable through the foreach statement and adds it to the ArrayList.
The third method is to bind Hashtable to the Dropdownlist. The advantage of Hashtable is that it can also bind two types of data: "key, and" value ". In this case, we can bind two different types of data to the dropdonwlist.
Program code Hashtable Ht = new Hashtable ();
Ht. Add ("January", "January ");
Ht. Add ("February", "February ");
Ht. Add ("March", "March ");
Ht. Add ("maid", "April ");
Ht. Add ("May", "May ");
Ht. Add ("June", "June ");
Ht. Add ("July", "July ");
This. DropDownList3.DataSource = Ht;
This. DropDownList3.DataValueField = "key ";
This. DropDownList3.DataTextField = "value ";
This. DropDownList3.DataBind ();
4th. Bind the Object to dropdownlist
First, add a class with the following structure:
Program code public class ClassMonth
{
Private string _ MonthEN = DateTime. Now. ToString ("MMMM", System. Globalization. CultureInfo. CreateSpecificCulture ("en "));
Private string _ MonthCN = DateTime. Now. ToString ("MMMM", System. Globalization. CultureInfo. CreateSpecificCulture ("zh-CN "));
Public ClassMonth ()
{
MonthCN = DateTime. Now. ToString ("MMMM", System. Globalization. CultureInfo. CreateSpecificCulture ("zh-CN "));
MonthEN = DateTime. Now. ToString ("MMMM", System. Globalization. CultureInfo. CreateSpecificCulture ("en "));
}
Public ClassMonth (string cn, string en)
{
MonthCN = cn; // The import variable is assigned a value for the attribute.
MonthEN = en; // The import variable is assigned a value for the attribute.
}
Public string MonthEN // Constructor
{
Get
{
Return _ MonthEN;
}
Set
{
_ MonthEN = value;
}
}
Public string MonthCN // Constructor
{
Get
{
Return _ MonthCN;
}
Set
{
_ MonthCN = value;
}
}
}
Binding method
Program code
ArrayList arlist = new ArrayList ();
Arlist. Add (new ClassMonth ("January", "January "));
Arlist. Add (new ClassMonth ("February", "February "));
Arlist. Add (new ClassMonth ("March", "March "));
Arlist. Add (new ClassMonth ("April", "CMDL "));
Arlist. Add (new ClassMonth ("May", "May "));
This. DropDownList4.DataSource = arlist;
This. DropDownList4.DataValueField = "MonthEN ";
This. DropDownList4.DataTextField = "MonthCN ";
This. DropDownList4.DataBind ();