C# 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 dual", 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+".";
                }
            }
        }

我们选用了CheckBoxList来实现爱好的收集和显示

1.收集时,将CheckBoxList里选中的项转换成字符串,并用“,”隔开
这里只要调用方法GetChecked(CheckBoxList checkList, string separator)
就可以获取到想要的数据。然后存入数据库。

2.显示时,先从库里获取爱好的数据(刚刚用“,”隔开的字符串),
 然后调用方法SetChecked(CheckBoxList checkList,string selval,string separator)
就可以将库里的数据用CheckBoxList的形式表现出来

......
方法的使用:
//这里获取CheckBoxList中的选中项并用","隔开
string str=GetChecked(this.checkList1, ",");
......
//这里是将str这个字符串的值又设回CheckBoxList
SetChecked(this.checkList1,str,",");

/// <summary>
        /// 初始化CheckBoxList中哪些是选中了的         /// </summary>
        /// <param name="checkList">CheckBoxList</param>
        /// <param name="selval">选中了的值串例如:"1,2,3,4,5,7"</param>
        /// <param name="parator">值串中使用的分割符例如"1,2,3,4,5,7"中的逗号【此处是标点符号】</param>
        public static string SetChecked(CheckBoxList checkList, string selval, string parator)
        {
            selval = parator + selval + parator;        //例如:"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 = parator + checkList.Items[i].Value + parator;
                if (selval.IndexOf(val) != -1)
                {
                    checkList.Items[i].Selected = true;
                    selval = selval.Replace(val, parator);        //然后从原来的值串中删除已经选中了的
                    if (selval == parator)        //selval的最后一项也被选中的话,此时经过Replace后,只会剩下一个分隔符
                    {
                        selval += parator;        //添加一个分隔符
                    }
                }
            }
            selval = selval.Substring(1, selval.Length - 2);        //除去前后加的分割符号
            return selval;
        }
      

获取选中项:

一:/// <summary>
        /// 获取选中CheckBoxList的值
        /// </summary>
        /// <param name="CBList">CheckBoxList 的ID</param>
        /// <returns>以","分隔的字符串</returns>
        public static string GetCheckBoxListValue(CheckBoxList CBList)
        {
            string strTemp = "";

            for (int i = 0; i < CBList.Items.Count; i++)
            {
                if (CBList.Items[i].Selected)
                {
                    if (strTemp == "")
                    {
                        strTemp = CBList.Items[i].Value;
                    }
                    else
                    {
                        strTemp += "," + CBList.Items[i].Value;
                    }
                }
            }

            return strTemp;
        }

二:

/// <summary>
        /// 得到CheckBoxList中选中了的值
        /// </summary>
        /// <param name="checkList">CheckBoxList</param>
        /// <param name="separator">分割符号</param>
        /// <returns></returns>
        public static string GetChecked(CheckBoxList checkList, string parator)
        {
            string selval = "";
            for(int i=0;i<checkList.Items.Count;i++)
            {
                if(checkList.Items[i].Selected)
                {
                    selval += checkList.Items[i].Value + parator;
                }
            }
            return selval;
        }

原文地址:https://www.cnblogs.com/suixufeng/p/3336154.html