在DropDownList中有值域和文本域,在程序开发中经常要确切的绑定两个域。在好多情况下,我们页面显示的是文本,而在数据库存储的是这种文本标 识的整数,如此可以利用Dictionary建立对象,将数据库存储的值与页面显示的值联系起来,进行简单的转换,并且这样还可以实现值或者文本的检索功 能。灵活,很适合进行程序开发。
关键代码:
1DropdownList 绑定hashTable,dictionary_DropDownList        DropDownList1.DataTextField = "value";
2DropdownList 绑定hashTable,dictionary_DropDownList        DropDownList1.DataValueField = "key";
3DropdownList 绑定hashTable,dictionary_DropDownList        DropDownList1.DataSource = getTypeList();
4DropdownList 绑定hashTable,dictionary_DropDownList        DropDownList1.DataBind();

实例代码:
DropdownList 绑定hashTable,dictionary_hashTable_05DropdownList 绑定hashTable,dictionary_hashTable_06
 1DropdownList 绑定hashTable,dictionary_DropDownList    protected void Button1_Click(object sender, EventArgs e)
 2DropdownList 绑定hashTable,dictionary_hashTable_08DropdownList 绑定hashTable,dictionary_DropDownList_09    {
 3DropdownList 绑定hashTable,dictionary_绑定_10        DropDownList1.DataTextField = "value";
 4DropdownList 绑定hashTable,dictionary_绑定_10        DropDownList1.DataValueField = "key";
 5DropdownList 绑定hashTable,dictionary_绑定_10        DropDownList1.DataSource = getTypeList();
 6DropdownList 绑定hashTable,dictionary_绑定_10        DropDownList1.DataBind();
 7DropdownList 绑定hashTable,dictionary_绑定_14    }

 8DropdownList 绑定hashTable,dictionary_DropDownList    public Dictionary<intstring> getTypeList()
 9DropdownList 绑定hashTable,dictionary_hashTable_08DropdownList 绑定hashTable,dictionary_DropDownList_09    {
10DropdownList 绑定hashTable,dictionary_绑定_10        Dictionary<intstring> list = new Dictionary<intstring>();
11DropdownList 绑定hashTable,dictionary_绑定_10        list.Add(1"L1用户");
12DropdownList 绑定hashTable,dictionary_绑定_10        list.Add(2"L2用户");
13DropdownList 绑定hashTable,dictionary_绑定_10        list.Add(3"赢富用户");
14DropdownList 绑定hashTable,dictionary_绑定_10        list.Add(4"股指期货用户");
15DropdownList 绑定hashTable,dictionary_绑定_10        return list;         
16DropdownList 绑定hashTable,dictionary_绑定_14    }

17DropdownList 绑定hashTable,dictionary_DropDownList    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
18DropdownList 绑定hashTable,dictionary_hashTable_08DropdownList 绑定hashTable,dictionary_DropDownList_09    {
19DropdownList 绑定hashTable,dictionary_绑定_10        Response.Write(DropDownList1.SelectedValue.ToString());
20DropdownList 绑定hashTable,dictionary_绑定_14    }