In DropDownList, there are value fields and text fields. In program development, it is often necessary to exactly bind two fields. In many cases, the text is displayed on our page, and the text recognition integer is stored in the database, so that you can use Dictionary to create an object, associate the value stored in the database with the value displayed on the page for simple conversion. In this way, you can also retrieve the value or text. Flexible and suitable for program development.
Key code: 1 DropDownList1.DataTextField = "value ";
2 DropDownList1.DataValueField = "key ";
3 DropDownList1.DataSource = getTypeList ();
4 DropDownList1.DataBind ();
Instance code:
1 protected void button#click (object sender, EventArgs e)
2 {
3 DropDownList1.DataTextField = "value ";
4 DropDownList1.DataValueField = "key ";
5 DropDownList1.DataSource = getTypeList ();
6 DropDownList1.DataBind ();
7}
8 public Dictionary <int, string> getTypeList ()
9 {
10 Dictionary <int, string> list = new Dictionary <int, string> ();
11 list. Add (1, "L1 user ");
12 list. Add (2, "L2 user ");
13 list. Add (3, "Win-rich users ");
14 list. Add (4, "Stock Index Futures users ");
15 return list;
16}
17 protected void dropdownlistincluselectedindexchanged (object sender, EventArgs e)
18 {
19 Response. Write (DropDownList1.SelectedValue. ToString ());
20}