CheckBoxList 显示,保存到数据库,从数据库读出来操作
1。界面显示
CheckBoxList1.DataSource = ds.Tables[0];//所有选项存到数据库
CheckBoxList1.DataTextField = "name";
CheckBoxList1.DataValueField = "id";
CheckBoxList1.DataBind();
2。保存到数据库
string name = "";
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (CheckBoxList1.Items[i].Selected)
{
name+= CheckBoxList1.Items[i].Value + "|";
}
}
if (name!= "")
{
name = name.Substring(0, otherdept.Length - 1);
}
然后把name保存到你的表的一列里。
3。从数据库读取
string name=从数据库读取保存的那列。
string[] names;
names= name.Split('|');
for (int i = 0; i < names.Length; i++)
{
CheckBoxList1.Items.FindByValue(names[i]).Selected = true;
}