http://www.cnblogs.com/hulang/archive/2010/12/29/1920662.html
|
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
一、DropDownList:
1、选项值保存到数据库:
Hashtable ht=newHashtable();//这里用Hashtable
ht.Add("字段名",DropDownListID.SelectedItem.Text.ToString());//保存选项Text
ht.Add("字段名",DropDownListID.SelectedItem.Value.ToString());//保存选项Value
2、选项值由数据库绑定到DropDownList:
首先DropDownListID.ClearSelection();//清除选项
DropDownListID.Items.FindByText(dr["字段名"].ToString()).Selected =true;//选项Text
DropDownListID.Items.FindByValue(dr["字段名"].ToString()).Selected =true;//选项Value
二、RadioButtonList:
1、选项值保存到数据库(同DropDownList):
Hashtable ht=newHashtable();//这里用Hashtable
ht.Add("字段名",RadioButtonListID.SelectedItem.Text.ToString());//保存选项Text
ht.Add("字段名",RadioButtonListID.SelectedItem.Value.ToString());//保存选项Value
2、选项值由数据库绑定到RadioButtonList
stringSelectItem = dr["字段名"].ToString();//将数据库中的选项值从DataRow中读出赋给变量SelectItem
for(inti = 0; i < RadioButtonListID.Items.Count; i++)
{//用for循环判断那项被选种
if(RadioButtonListID.Items[i].Text == SelectItem)RadioButtonListID.Items[i].Selected =true;
}
三、CheckBoxList:
1、选项值保存到数据库
stringSelectItem ="";//声明一个变量来接受选项
for(inti = 0; i < CheckBoxListID.Items.Count; i++)
{//用for循环将所有选项用","隔开连接起来
if(CheckBoxListID.Items[i].Selected)
{
SelectItem = SelectItem + CheckBoxListID.Items[i].Value +",";//选项后加","隔开
}
}
ht.Add("字段名",SelectItem.ToString());
2、选项值由数据库绑定到CheckBoxList
stringSelectItem = dr["字段名"].ToString();
string[] arrStr = SelectItem.Split(',');//字段是以","隔开
foreach(stringstrinarrStr)
{
for(inti = 0; i <CheckBoxListID.Items.Count; i++)
{
if(this.CheckBoxListID.Items[i].Value == str)
{
this.CheckBoxListID.Items[i].Selected =true;
}
}
}
=================================================
1.把数据绑定到CheckBoxList中
protectedvoidPage_Load(objectsender, EventArgs e)
{
if(!Page.IsPostBack)
{
SqlConnection con = GetDBCon.GetCon();
con.Open();
SqlDataAdapter sda =newSqlDataAdapter("select * from admin", con);
DataSet ds =newDataSet();
sda.Fill(ds,"admin");
this.CheckBoxList1.DataSource = ds.Tables[0];
this.CheckBoxList1.DataTextField ="username";//绑定的字段名
this.CheckBoxList1.DataValueField ="userid";//绑定的值
this.CheckBoxList1.DataBind();
}
}
2.循环读取出来
protectedvoidCheckBoxList1_SelectedIndexChanged(objectsender, EventArgs e)
{
this.Lab2.Text ="";
for(inti = 0; i < CheckBoxList1.Items.Count; i++)
{
if(this.CheckBoxList1.Items[i].Selected)
{
this.Lab2.Text =this.Lab2.Text+CheckBoxList1.Items[i].Text+".";
}
}
}
|
转载于:https://www.cnblogs.com/oshoh/p/6970905.html
今天的文章DropDownList绑定及修改分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/25749.html