Bind hashTable and dictionary to DropdownList

Source: Internet
Author: User
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 ();
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;
17 protected void dropdownlistincluselectedindexchanged (object sender, EventArgs e)
18 {
19 Response. Write (DropDownList1.SelectedValue. ToString ());

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.