CheckBoxList1获取选中项和绑定选中项

页面添加一个CheckBoxList1,后台进行数据绑定,关键代码如下:

 1 DataSet ds = gl.GetList("(isdelete = 0 or isdelete is null) AND (iscost=0 or iscost is null)");
 2             CheckBoxList1.DataSource = ds;
 3             CheckBoxList1.DataTextField = "lcmc";
 4             CheckBoxList1.DataValueField = "id";
 5             CheckBoxList1.DataBind();
 6             foreach (ListItem lst in this.CheckBoxList1.Items)
 7             {
 8                 YHGLDS model = YH.GetModel(1);
 9 
10                 string[] arr = model.xzcf_qx.ToString().Trim().Split(',');
11                 for (int i = 0; i < arr.Length; i++)
12                 {
13                     if (arr[i].ToString().Equals(lst.Value))
14                         lst.Selected = true;
15                 }
16             }

绑定之后,保存时进行选中的保存 ,关键代码如下:

 1  YHGLDS model = YH.GetModel(1);
 2             string qxsz = "";
 3             foreach (ListItem lst in this.CheckBoxList1.Items)
 4             {
 5 
 6                     if (lst.Selected == true)
 7                     {
 8                         qxsz += lst.Value+",";
 9                         
10                     }
11                         
12                 
13             }
14             model.xzcf_qx = qxsz.Trim(',');

 接下来便是可以直接显示,进行使用了;

原文地址:https://www.cnblogs.com/njccqx/p/5795535.html