public class CheckListboxHelper { #region 为checklistbox绑定数据源 /// <summary> /// 为checklistbox绑定数据源 /// </summary> /// <typeparam name="T">数据源类型</typeparam> /// <param name="checklistbox">checklistbox对象</param> /// <param name="list">数据源</param> /// <param name="texfield">显示值字段名称</param> /// <param name="valuefield">值字段名称</param> public void CheckListboxDatabind<T>(CheckBoxList checklistbox, List<T> list, string texfield, string valuefield) { checklistbox.DataSource = list; checklistbox.DataTextField = texfield; checklistbox.DataValueField = valuefield; checklistbox.DataBind(); } #endregion #region 获取checklistbox选中的值 /// <summary> /// 获取checklistbox选中的值 /// </summary> /// <param name="checklistbox">checklistbox对象</param> /// <returns>string字符串</returns> public string GetChecklistvalue(CheckBoxList checklistbox) { string listvalue = ""; for (int i = 0; i < checklistbox.Items.Count; i++) { if (checklistbox.Items[i].Selected) { listvalue = listvalue + checklistbox.Items[i].Value + ","; } } listvalue = new StringHelper().ReplaceLastchar(listvalue, ","); return listvalue; } #endregion #region 设置checklistboxbox选中的值 public void SetCheckListBoxChecked(CheckBoxList checklistbox, string checkvale) { string[] checkstrs; checkstrs = checkvale.Split(','); for (int i = 0; i < checklistbox.Items.Count; i++) { for (int j = 0; j < checkstrs.Length; j++) { if (checklistbox.Items[i].Value == checkstrs[j]) { checklistbox.Items[i].Selected = true; break; } } } } #endregion #region 设置checkboxlist全选 public void SetCheckListBoxAllChecked(CheckBox controlChecklistbox, CheckBoxList checklistbox) { for (int i = 0; i < checklistbox.Items.Count; i++) { checklistbox.Items[i].Selected = controlChecklistbox.Checked; } } #endregion }
今天要使用checklistboxbox,对其常用的方法进行了封装,有需要的朋友可以使用,也算是对自己写的东西的记录。
新闻热点
疑难解答