得到CheckBoxList选中了的值存入数据库,并取出显示页面上

来源:互联网 发布:微霸软件效果 编辑:程序博客网 时间:2024/06/05 08:58

    /// <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;
    }

//调用GetChecked方法的使用

string cbl_ICBCLevel = GetChecked(this.cbl_ICBCLevel, ",");//定义的变量cbl_ICBCLevel存入数据库

//从数据库中取出并显示在页面上

            string ICBCLevel = dt2.Rows[0]["cICBCLevel"].ToString();//从数据库读取值
            string[] str=ICBCLevel.Split(',');//调用Split方法,定义string[]类型的变量
            for (int i = 0; i < str.Length; i++)
            {
                for (int j = 0; j < cbl_ICBCLevel.Items.Count; j++)//循环遍历在页面中CheckBoxList里的所有项
                {
                    if (this.cbl_ICBCLevel.Items[j].Value == str[i])//判断i遍历后的id与cbx当中的值是否有相同
                    {
                        this.cbl_ICBCLevel.Items[j].Selected = true; //相同就选中
                    }
                }

            }

 

原创粉丝点击