DropDownList WEB server controls enable users to select an item from a predefined list. It differs from the ListBox Web server control in that its list of items remains hidden until the user clicks the Drop-down button. Also, the difference between a DropDownList control and a ListBox control is that it does not support multiple selection modes.
DropDownList the rendering in HTML corresponds to select, let's take a look at several methods of DropDownList binding data.
First, bind array array to DropDownList
Copy Code code as follows:
String[] Month =new string[7]{"January", "February", "March", "April", "may", "June", "July"};
This. Dropdownlist1.datasource = Month;
This. Dropdownlist1.databind ();
This method can only bind a set of data to DropDownList, because DropDownList can bind two kinds of data: 1 is DataTextField, 2 is DataValueField, so the first method binds the DataTextField value = = DataTextField value.
Second, bind the dynamic array array to DropDownList
Copy Code code as follows:
ArrayList ar = new ArrayList ();
for (int i = 1; I <=12; i++)
{
Ar. ADD (i+ "month");
}
This. Dropdownlist2.datasource = AR;
This. Dropdownlist2.databind ();
In essence, January-December is added to the array as follows:
Copy Code code as follows:
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 this approach is that you can implement dynamic addition of elements by Arraylist.add method, for example, with a DataTable, we want to read the data from one row of the DataTable to the ArrayList.
Look at the code I showed below.
Copy Code code as follows:
ArrayList ar = new ArrayList ();
DataTable Dt=dataset. Tables[0]
foreach (DataRow dr in Dt. Rows)
{
Ar. ADD (Dr[0]. ToString ());
}
The above code is added to the ArrayList from a DataTable by iterating through the value of the first cell in a row of data in a table through a foreach statement.
The advantage of the method of binding Hashtable to DropDownList Hashtable is that it can bind two kinds of data, one is "key" and the other is "value", so that we can bind two different data for dropdonwlist.
Copy Code code as follows:
Hashtable Ht = new Hashtable ();
Ht.add ("January", "January");
Ht.add ("February", "February");
Ht.add ("March", "March");
Ht.add ("April", "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 ();
Binding object objects to DropDownList
First, add a class, which is structured as follows
Copy Code code as follows:
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;//Import Variable to property assignment
Monthen = en;//Import Variable to property assignment
}
public string Monthen//Construction Properties
{
Get
{
return _monthen;
}
Set
{
_monthen = value;
}
}
public string MONTHCN//Construction Properties
{
Get
{
return _MONTHCN;
}
Set
{
_MONTHCN = value;
}
}
}
Binding method
Copy Code code as follows:
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", "April"));
Arlist. ADD (New Classmonth ("May", "may"));
This. Dropdownlist4.datasource = arlist;
This. Dropdownlist4.datavaluefield = "Monthen";
This. Dropdownlist4.datatextfield = "MONTHCN";
This. Dropdownlist4.databind ();