[ASP.NET] 将数据绑定到DropDownlist中的常见问题

如何为dropdownlist设定默认选中项

DropDownList1为DrowDownList的id,productType为选项内容
DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText(productType));

如何为dropdownlist绑定数据

以下内容引用自 http://www.2cto.com/os/201504/395460.html ,使用ArrayList的时候应该是要有key和value,不然会报错。

第一种,把Array数组绑到dropdownlist

程序代码
?
1
2
3
string[] Month = new string[ 7 ]{ "January" , "February" , "March" , "April" , "May" , "June" , "July" };
this .DropDownList1.DataSource = Month;
this .DropDownList1.DataBind();


第一种方法只可以绑定一组数据到dropdownlist,因为drawdonwlist可以绑定两种数据1是DataTextField
2是DataValueField 所以第一种方法绑定后DataTextField的值==DataTextField值

第二种,把Array数组绑定到dropdownlist

程序代码
?
1
2
3
4
5
6
7
8
9
ArrayList ar = new ArrayList();
for ( int i = 1 ; i <= 12 ; i++)
{
ar.Add(i+ "月" );
 
}
 
this .DropDownList2.DataSource = ar;
this .DropDownList2.DataBind();


直观一点的写法。
程序代码
?
1
2
3
4
5
6
7
8
9
ArrayList ar = new ArrayList();
ar.Add( "1月" );
ar.Add( "2月" );
ar.Add( "3月" );
ar.Add( "4月" );
..................................................
 
this .DropDownList2.DataSource = ar;
this .DropDownList2.DataBind();


第2种方法的好处是通过ArrayList.Add的方法,可以实现动态添加元素的功能,比方说,有一个DataTable,我们要把DataTable中一行的数据读出来添加到Arraylist当中。

看我以下的示的代码

程序代码

?
1
2
3
4
5
6
7
8
9
ArrayList ar = new ArrayList();
DataTable dt=dataset.Tables[ 0 ]
foreach (DataRow dr in dt.Rows)
{
 
ar.Add(dr[ 0 ].ToString());
 
 
}

以上代码从一个DataTable中通过foreach语句循环读取Table中一行数据中第一个格的值添加到ArrayList当中。

第三种方法,将Hashtable绑定到Dropdownlist当中Hashtable的方法的好处是,它也可以绑定两种数据一个是"key,一个是"value",这样的话,我们就可以为dropdonwlist绑定上两种不同的数据了。

程序代码
?
1
2
3
4
5
6
7
8
9
10
11
12
Hashtable Ht = new Hashtable();
Ht.Add( "January" , "1月" );
Ht.Add( "February" , "2月" );
Ht.Add( "March" , "3月" );
Ht.Add( "April" , "4月" );
Ht.Add( "May" , "5月" );
Ht.Add( "June" , "6月" );
Ht.Add( "July" , "7月" );
this .DropDownList3.DataSource = Ht;
this .DropDownList3.DataValueField = "key" ;
this .DropDownList3.DataTextField = "value" ;
this .DropDownList3.DataBind();




第4种,把Object对象绑定到dropdownlist

首先新增一个类,结构如下

程序代码
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
public class ClassMonth
{
private string _MonthEN = DateTime.Now.ToString( "MMMM" ,System.Globalization.CultureInfo.CreateSpecificCulture( "en" ));
private string _MonthCN = DateTime.Now.ToString( "MMMM" , System.Globalization.CultureInfo.CreateSpecificCulture( "zh-CN" ));
public ClassMonth()
{
 
MonthCN = DateTime.Now.ToString( "MMMM" , System.Globalization.CultureInfo.CreateSpecificCulture( "zh-CN" ));
MonthEN = DateTime.Now.ToString( "MMMM" , System.Globalization.CultureInfo.CreateSpecificCulture( "en" ));
}
 
 
 
public ClassMonth(string cn,string en)
{
MonthCN = cn; //导入变量为属性赋值
MonthEN = en; //导入变量为属性赋值
 
}
 
public string MonthEN //构造属性
{
get
{
return _MonthEN;
 
}
set
{
 
_MonthEN = value;
 
}
 
 
 
}
public string MonthCN //构造属性
{
get
{
return _MonthCN;
 
}
set
{
 
_MonthCN = value;
 
}
 
 
}
}


绑定方法

程序代码

?
1
2
3
4
5
6
7
8
9
10
11
ArrayList arlist= new ArrayList();
arlist.Add( new ClassMonth( "1月" , "January" ));
arlist.Add( new ClassMonth( "2月" , "February" ));
arlist.Add( new ClassMonth( "3月" , "March" ));
arlist.Add( new ClassMonth( "4月" , "April" ));
arlist.Add( new ClassMonth( "5月" , "May" ));
 
this .DropDownList4.DataSource = arlist;
this .DropDownList4.DataValueField = "MonthEN" ;
this .DropDownList4.DataTextField = "MonthCN" ;
this .DropDownList4.DataBind();

 

 

转载于:https://www.cnblogs.com/loxay/p/4936652.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值