如何在webpart中将选择字段保存到Sharepoint列表?

时间:2013-03-16 08:14:44

标签: sharepoint-2010 checkboxlist multiple-choice choicefield

我有一个SharepointList

列表名称:RegionList 字段:regId number         Regname Choice (CheckBox:允许多项选择)

Choice字段项显示在CheckBoxList项中。 我将这些项目保存为逗号分隔值的字符串。

protected string GetSelectedRegions()
        {
            List<String> regList = new List<string>();
            // Loop through each item.
            foreach (ListItem item in chkRegion.Items)
            {
                if (item.Selected)
                {
                    // If the item is selected, add the value to the list.
                    regList.Add(item.Value);
                }
                else
                {
                    // Item is not selected, do something else.
                }
            }

            String regs = String.Join(",", regList.ToArray());
            return regs;
        }

从上面的代码中,regs参数包含所选项目的数量并保存到List。 现在,问题是当我打开列表并在Edit模式下打开记录然后CHOICE Field Doesn't Show any Selected ITEM. But, when i send only single value then it Show the Selected Item that was saved.

任何想法? Plz让我知道如何将CheckBoxList项目存储到CHOICE字段并进行回收。提前致谢!

1 个答案:

答案 0 :(得分:1)

对于set multi-Checkbox,您应该使用SPFieldMultiChoiceValue,如下所示:

protected SPFieldMultiChoiceValue GetSelectedRegions()
     {
        SPFieldMultiChoiceValue multiValue = new SPFieldMultiChoiceValue();

         List<String> regList = new List<string>();
        // Loop through each item.
        foreach (ListItem item in chkRegion.Items)
        {
            if (item.Selected)
            {
                // If the item is selected, add the value to the list.
                multiValue.Add(item.Value);
            }
            else
            {
                // Item is not selected, do something else.
            }
        }

        //String regs = String.Join(",", regList.ToArray());
        return multiValue;
    }

而不是将SPFieldMultiChoiceValue设置为您的SPListItem

  item["multivalued choice field name"]= GetSelectedRegions();