zoukankan
html css js c++ java
DropdownList绑定hashTable,dictionary
在DropDownList中有值域和文本域,在程序开发中经常要确切的绑定两个域。在好多情况下,我们页面显示的是文本,而在数据库存储的是这种文本标识的整数,如此可以利用Dictionary建立对象,将数据库存储的值与页面显示的值联系起来,进行简单的转换,并且这样还可以实现值或者文本的检索功能。灵活,很适合进行程序开发。
关键代码:
1
DropDownList1.DataTextField
=
"
value
"
;
2
DropDownList1.DataValueField
=
"
key
"
;
3
DropDownList1.DataSource
=
getTypeList();
4
DropDownList1.DataBind();
实例代码:
==Code==
1
protected
void
Button1_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用户
"
);
12
list.Add(
2
,
"
L2用户
"
);
13
list.Add(
3
,
"
赢富用户
"
);
14
list.Add(
4
,
"
股指期货用户
"
);
15
return
list;
16
}
17
protected
void
DropDownList1_SelectedIndexChanged(
object
sender, EventArgs e)
18
{
19
Response.Write(DropDownList1.SelectedValue.ToString());
20
}
1
查看全文
相关阅读:
C语言寒假大作战04
C语言寒假大作战03
C语言寒假大作战02
C语言寒假大作战01
C语言I作业12—学期总结
C语言I博客作业11
C语言I博客作业10
预习非数值数据的编码方式
计算机组成与系统结构作业01
C语言||作业01
原文地址:https://www.cnblogs.com/yank/p/1052931.html
最新文章
预习原码补码
C语言ll作业01
C语言寒假大作战04
C语言寒假大作战03
C语言寒假大作战02
C语言寒假大作战01
“小黄衫”获奖感言
C语言I作业12—学期总结
运算方法和运算部件
预习非数值数据的编码表示
热门文章
预习原码补码
C语言II作业01
C语言寒假大作战04
C语言寒假大作战03
C语言寒假大作战02
C语言寒假大作战01
C语言I作业12—学期总结
预习非数值数据的编码方式
预习原码补码+作业
C语言ll作业01
Copyright © 2011-2022 走看看