设置或者获取CheckboxList控件的选中值

 1.设置CheckBoxList选中的值

   /// <summary>
        /// 设置CheckBoxList中哪些是选中了的         
        /// </summary>
        /// <param name="checkList">CheckBoxList</param>
        /// <param name="selval">选中的字符串,例如:"0,1,2,3,4,4,24"</param>
        /// <param name="separator">分割字符</param>
        public void SetChecked(CheckBoxList checkList, string str, string separator)
        {
            for (int i = 0; i < checkList.Items.Count; i++)
            {
                //checkboxList 列表中的选项值
                string val = checkList.Items[i].Value;
                if (val != "" && val!=null)
                {
                    if (SplitArr(val, str, separator) >= 0)
                    {
                        checkList.Items[i].Selected = true;
                    }
                }
            }
        }

        public int SplitArr(string val, string strs, string separator)
        {
            string[] arr = strs.Split(separator);
            for (int i = 0; i < arr.Length; i++)
            {
                if (val == arr[i])
                {
                    return i;
                }
            }
            return -1;
        }

2.获取CheckboxList选中了的值

 /// <summary>
        /// 获取checkboxlist的值
        /// </summary>
        /// <param name="checkList"></param>
        /// <param name="separator"></param>
        /// <returns></returns>
        public 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;
        }

 

 

posted @ 2013-10-07 18:18  学之乐  阅读(1235)  评论(0编辑  收藏  举报