DropDownList 与Hashtable之间梆定
1
/// <summary>
2
/// 功能:DropDownList与Hashtable数据的梆定
3
///
4
/// 这是一个很特殊的一个列字,它们之间梆
5
///
6
/// 定只是梆定键与值就可以了
7
///
8
/// 时间:二00八年一月二十五日
9
///
10
/// 作者:曹代明
11
/// </summary>
12
using System;
13
using System.Data;
14
using System.Configuration;
15
using System.Web;
16
using System.Web.Security;
17
using System.Web.UI;
18
using System.Web.UI.WebControls;
19
using System.Web.UI.WebControls.WebParts;
20
using System.Web.UI.HtmlControls;
21
using System.Collections;
22
23
public partial class _Default : System.Web.UI.Page
24
{
25
protected void Page_Load(object sender, EventArgs e)
26
{
27
if (IsPostBack == false)
28
{
29
Hashtable ht = new Hashtable();
30
ht.Add("成都","028");
31
ht.Add("西安","029");
32
ht.Add("北京","010");
33
34
DropDownList1.DataSource = ht;
35
DropDownList1.DataTextField = "key"; //数据字段就是键
36
DropDownList1.DataValueField = "value"; //数据源就是value值
37
DropDownList1.DataBind();
38
}
39
}
40
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
41
{
42
Response.Write(this.DropDownList1.SelectedValue.ToString());
43
}
44
}
45
/// <summary>2
/// 功能:DropDownList与Hashtable数据的梆定3
/// 4
/// 这是一个很特殊的一个列字,它们之间梆5
/// 6
/// 定只是梆定键与值就可以了7
/// 8
/// 时间:二00八年一月二十五日9
/// 10
/// 作者:曹代明11
/// </summary>12
using System;13
using System.Data;14
using System.Configuration;15
using System.Web;16
using System.Web.Security;17
using System.Web.UI;18
using System.Web.UI.WebControls;19
using System.Web.UI.WebControls.WebParts;20
using System.Web.UI.HtmlControls;21
using System.Collections;22

23
public partial class _Default : System.Web.UI.Page 24
{25
protected void Page_Load(object sender, EventArgs e)26
{27
if (IsPostBack == false)28
{29
Hashtable ht = new Hashtable();30
ht.Add("成都","028");31
ht.Add("西安","029");32
ht.Add("北京","010");33

34
DropDownList1.DataSource = ht;35
DropDownList1.DataTextField = "key"; //数据字段就是键36
DropDownList1.DataValueField = "value"; //数据源就是value值37
DropDownList1.DataBind();38
}39
}40
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)41
{42
Response.Write(this.DropDownList1.SelectedValue.ToString());43
}44
}45

这是一种很特殊的一种梆定,这也许是Hashtable的一种独特的地方,这样只是梆定他们的
key,value就可以了,这样对我们在开发中有很大的帮助



浙公网安备 33010602011771号