asp.net的checkboxlist绑定数据
1.把数据绑定到CheckBoxList中
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SqlConnection con = GetDBCon.GetCon();
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("select * from admin", con);
DataSet ds = new DataSet();
sda.Fill(ds,"admin");
this.CheckBoxList1.DataSource = ds.Tables[0];
this.CheckBoxList1.DataTextField = "username";//绑定的字段名
this.CheckBoxList1.DataValueField = "userid";//绑定的值
this.CheckBoxList1.DataBind();
}
}
2.循环读取出来
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
this.Lab2.Text = "";
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (this.CheckBoxList1.Items[i].Selected)
{
this.Lab2.Text = this.Lab2.Text+CheckBoxList1.Items[i].Text+".";
}
}
}
#region 设置或者得到CheckBoxList选中了的值
/**//// <summary>
/// 初始化CheckBoxList中哪些是选中了的 /// </summary>
/// <param name="checkList">CheckBoxList</param>
/// <param name="selval">选中了的值串例如:"0,1,1,2,1"</param>
/// <param name="separator">值串中使用的分割符例如"0,1,1,2,1"中的逗号</param>
public static string SetChecked(CheckBoxList checkList,string selval,string separator)
...{
selval = separator + selval + separator; //例如:"0,1,1,2,1"->",0,1,1,2,1,"
for(int i=0; i<checkList.Items.Count; i++)
...{
checkList.Items[i].Selected = false;
string val = separator + checkList.Items[i].Value + separator;
if(selval.IndexOf(val)!=-1)
...{
checkList.Items[i].Selected = true;
selval = selval.Replace(val,separator); //然后从原来的值串中删除已经选中了的
if(selval == separator) //selval的最后一项也被选中的话,此时经过Replace后,只会剩下一个分隔符
...{
selval += separator; //添加一个分隔符
}
}
}
selval = selval.Substring(1,selval.Length-2); //除去前后加的分割符号
return selval;
}
/**//// <summary>
/// 得到CheckBoxList中选中了的值
/// </summary>
/// <param name="checkList">CheckBoxList</param>
/// <param name="separator">分割符号</param>
/// <returns></returns>
public static string GetChecked(CheckBoxList checkList, string separator)
...{
string selval = "";
for(int i=0;i<checkList.Items.Count;i++)
...{
if(checkList.Items[i].Selected)
...{
selval += checkList.Items[i].Value + separator;
}
}
return selval;
}
#endregion
样式设置:
RepeatColumns="3" 列数
RepeatLayout="Table" table布局方式
RepeatDirection="Horizontal"
或
RepeatDirection="Vertical"