Back to previous

How to retrieve and update from a multi choice Checkboxlist field

To create a checkboxlist from a multi choice field:

//create a check box list

SPFieldMultiChoice choices = (SPFieldMultiChoice)oList.Fields[strFieldName];
foreach (string str in choices.Choices){
      cblCheckboxList.Items.Add(new ListItem(str, str));
}

//mark those selected

if (enquiry[“Reasons”] != null){
     SPFieldMultiChoiceValue itemValue = new SPFieldMultiChoiceValue(enquiry[“Reasons”]);
     for (int i=0; i<= itemValue.Count; i++)
          cblCheckboxList.Items.FindByText(itemValue[i]).Selected = true;
}

//To update

SPFieldMultiChoiceValue choices = new SPFieldMultiChoiceValue();
foreach (ListItem ls in cblCheckboxList.Items){
      if (ls.Selected) choices.Add(ls.Value);
}
Enquiry[“Reasons”] = choices;

ShareThis

If you think this post is useful, please recommend me at the bottom of the page. ;)

Discussion