数据绑定:
SqlConnection con = new SqlConnection("server=.;database=diancai;uid=sa;pwd=;");
con.Open();
//7元荤菜
SqlCommand cmd1 = new SqlCommand("select * from CaiXinxi where caiShuxing='荤' and caiSpr=0 and (select count(*) from CaiXinxi where caiXingqi like '%" + LabelDay.Text + "%')>0", con);
SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
DataSet ds1 = new DataSet();
da1.Fill(ds1);
CheckBoxList7hun.DataSource = ds1;
CheckBoxList7hun.DataTextField = "caiMingcheng";
CheckBoxList7hun.DataBind();
con.Close();
判断CheckBoxList被选中的数量:
int count2 = 0;
foreach (ListItem litem in CheckBoxListsu.Items)
{
if (litem.Selected)
{
count2 ++;
}
}
if (count2 != 2)
{
Response.Write("<script>alert(/"素菜要两份哦!!/");</script>");
return;
}
//选中的是哪两个菜
string m_strTemp = string.Empty;
for (int i = 0; i < CheckBoxListsu.Items.Count; i++)//读取CheckBoxList 选中的值,保存起来
{
if (CheckBoxListsu.Items[i].Selected)
{
m_strTemp += CheckBoxListsu.Items[i].Value + "','";
}
}
当有多个chackbox被选中,写入数据库时:
SqlCommand cmd1 = new SqlCommand("insert into Diancai (hun1,su1,su2,yinliao) values('" + CheckBoxList7hun.SelectedValue + "','" + m_strTemp + CheckBoxListyinliao.SelectedValue + "')", con);
cmd1.ExecuteNonQuery();