1.設置CheckBoxList選中的值spa
/// <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選中了的值code
/// <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; }