开发者

Only Alternating values in SPFieldMultiChoice saved

I am busy building a custom webpart with various textboxes and lookup fields. All of them are saving correctly apart from the lookup fields that allows for multiple selections. I dont have this problem with lookup fields that only allow for one value to be selected.

Below is the code for getting all the selected items in my checkboxlist, convert开发者_StackOverflow中文版ing it to a multichoice value and assigning to my list[columnname]

try
{
    SPFieldMultiChoiceValue _segmentchoices = new SPFieldMultiChoiceValue();
    foreach (ListItem ls3 in _segment.Items)
    {
        if (ls3.Selected) _segmentchoices.Add(ls3.Value);
    }
    myItems["Segment"] = _segmentchoices;
    myItems.Update();
}
catch (Exception ex) { _errorMessage += "||| Segment : " + ex.Message; }

The values list (_segmentchoices) is correctly created and looks like this : {;#1;#2;#3;#4;#5;#}

However when its saved it only saves values 1, 3, and 5.

My code is not generating an error, so I am at a loss at what could be wrong. Any ideas on what I need to look at? Am I going about it the wrong way?

Any assistance would be appreciated. Thank you


I just realized you are talking about a multi-select lookup field. The format should be something like: 2;#Procedures;#3;#Systems;#7;#Services

The behavior you describe makes sense because it is probably interpreting ;#1;#2;#3;#4;#5;# like this: get the item in the lookup list with a lookupID of 1 (lookupValue is 2), lookupID of 3 (lookupValue is 4), and lookupID of 5 (lookupValue is empty)

Here is some code that you can use to update a multi-select choice field:

using (SPSite site = new SPSite(siteUrl))
{
    using (SPWeb web = site.OpenWeb())
    {
        SPList list = web.Lists[listName];
        SPListItem item = list.Items[0];

        SPFieldLookupValueCollection spflvc = new SPFieldLookupValueCollection();
        spflvc.Add(new SPFieldLookupValue(3, string.Empty));
        spflvc.Add(new SPFieldLookupValue(7, string.Empty));
        item["Keywords"] = spflvc;
        item.Update();
    }
}

The second parameter to SPFieldLookupValue doesn't seem to care if it is passed string.Empty which also might explain why it ignores them above.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜