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 the page, while the integer of the text identifier is stored in the database, so that the object can be created using dictionary, 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: = 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}
1