Minor modifications - sorting, clear text from calc. fields, current field value always included

Mar 27, 2009 at 4:45 PM
  1. Values in lookup are always ordered by text, instead of using whatever ordered is set up in query/view
  2. When the lookup points to calculated field, "string;#" is always prepended to the title
  3. Imagine you use the lookup to filter out old items. Then one day, you need to change an item that has a filtered lookup and it's value now falls into the "old" category and so is not offered in the list. When you save the item, you loose the filtered lookup's data. Solution - always include current value.
All these three fall into fieldcontrol.Initialize() and can be solved like this (which may be a bit clumsy, but works for me, at least currently)

if ((items != null && items.Count > 0))
                _availableItems = items
                  // lookup to a calculated field shows "string;#" in front of every title, get rid of it
                  .Select(e => new ListItem(((e[new Guid(f.LookupField)].ToString().StartsWith("string;#")) ? e[new Guid(f.LookupField)].ToString().Substring(8) : e[new Guid(f.LookupField)].ToString()), e.ID.ToString()))

                if (_fieldVal != null)
                    bool currValContained = false;
                    foreach (ListItem it in _availableItems)
                        if (_fieldVal.LookupId.ToString() == it.Value)
                            currValContained = true;

                    if (!currValContained)
                        _availableItems.Add(new ListItem(_fieldVal.LookupValue, _fieldVal.LookupId.ToString()));

                // use whatever sorting is defined in the query/view instead of forcing ABC
                // custom sorting can be just as handy as filtering
                /*_availableItems.Sort(delegate(ListItem item1, ListItem item2) {
                  return item1.Text.CompareTo(item2.Text);