后台绑定checkboxlist,前台如何获取checkboxlist选中的值
2013-12-16
//cblForUser这是 checkboxlist的ID
//bindForUser()引用放在ShowInfo()前面
checkboxlis protected void bindForUser()//生成动态前台复选框。
{
using (
SqlDataReader sdr =
DbHelperSQL.ExecuteReader(
"select id, DName from TDepartment"))
{
cblForUser.DataSource = sdr;
cblForUser.DataTextField = "DName";
cblForUser.DataValueField = "DName";
cblForUser.RepeatDirection = RepeatDirection.Horizontal;
cblForUser.RepeatColumns = 5;
cblForUser.DataBind();
ListItem li = new ListItem("所有部门", "0");
cblForUser.Items.Insert(0, li);
}
}
protected string getForUser()
{
string strForUser = "";
for (int i = 0; i < cblForUser.Items.Count; i++)
{
if (cblForUser.Items[i].Selected)
strForUser += cblForUser.Items[i].Value + ",";
}
return strForUser;
}
private void ShowInfo(string LoginID)
{
using (SqlDataReader sdr = DbHelperSQL.ExecuteReader("select Alldepartments from TEmployee"))//这个是你添加的时候保存选项的值
{
if (sdr.Read())
{
string strForUser = sdr.GetValue(0).ToString();//把Alldepartments 的值转换为string类型
string[] ForUser = strForUser.Split(',');
for (int i = 0; i < ForUser.Length; i++)
{
if (ForUser[i].Trim() != "")
{
cblForUser.Items.FindByValue(ForUser[i]).Selected = true;//把选中的值打钩 }
}
}
}
}