/// <summary> ///CheckBoxListHelper 的摘要说明 ///CheckBoxList获取与设置选中的值 /// </summary> public class CheckBoxListHelper { private CheckBoxListHelper() { // //TODO: 在此处添加构造函数逻辑 // } /// <summary> /// 值的分割符 /// </summary> private const string SEPARATOR=","; /// <summary> /// 获取CheckBoxList被选中的值 /// </summary> /// <param name="cblist"></param> /// <returns></returns> public static string GetCheckBoxListCheckValue(CheckBoxList cbList) { if (cbList == null) return ""; System.Text.StringBuilder builder = new System.Text.StringBuilder(); foreach (ListItem item in cbList.Items) { if (item.Selected) { builder.AppendFormat("{0}{1}", item.Value,SEPARATOR); } } if (builder.Length > 0) { builder.Remove(builder.Length - 1, 1); } return builder.ToString(); } /// <summary> /// 设置CheckBoxList选中的值 /// </summary> /// <param name="cbList"></param> /// <returns></returns> public static void SetCheckBoxListCheck(CheckBoxList cbList,string values) { //当没有选择值时,取消所有选择项 if (string.IsNullOrEmpty(values)) values=SEPARATOR; if (cbList == null) return; //例如1,2,3 变为 1,2,3, values = values + SEPARATOR; foreach (ListItem item in cbList.Items) { item.Selected = false;//取消被选择 string value = item.Value; if (values.IndexOf(value + SEPARATOR) > -1) { item.Selected = true; } } } }