asp.Netcheckboxlistバインドデータ読み出し

1033 ワード

1.CheckBoxにデータをバインドする
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+".";

        }

    }

}