• 绑定或设置或者得到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+".";
                    }
                }
            }

    我们选用了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">选中了的值串例如:"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;
            }

  • 相关阅读:
    hdu1875(最小生成树prime)
    hdu1839(最小生成树)
    poj2739(尺取法+质数筛)
    poj2100(尺取法)
    codeforces-div2-449-B
    gym-101350M
    gym-10135I
    gym-101350H
    gym-101350D
    hdu 5569
  • 原文地址:https://www.cnblogs.com/ymyglhb/p/1486575.html
Copyright © 2020-2023  润新知