頁面添加一個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(',');
接下來便是可以直接顯示,進行使用了;