I have created custom field type inherited from SPFieldText successfully. I save a data in the item value as string of the next format: "id;#title". Now I need to convert the base type from SPFieldText to SPFieldLookup. But I have a problem with saving values in ItemFieldValue when updating field.
Here is my code of updating SPListItem:
public override void UpdateFieldValueInItem()
{
base.UpdateFieldValueInItem();
try
{
EnsureChildControls();
var field = Field as RecordLookupField; // my custom field
if (!String.IsNullOrEmpty(_lookupFieldValues.Value) &&
!String.IsNullOrEmpty(_mainTextBx.Text))
{
if (field != null)
Item[field.RelatedField] = SPContext.Current.Web.Lists[Settings.ListId].
Fields[Settings.MainDisplayColumn].InternalName;
var id = Convert.ToInt32(_lookupFieldId.Value);
var value = _lookupFieldValue.Value;
ItemFieldValue = new SPFieldLookupValue(id,value).ToString();
}
else
{
ItemFieldValue = null;
Item[Field.RelatedField] = null;
}
}
catch (Exception ex)
{
ex.Trace(GetType());
}
}
This code works without exception. But when i try to save my field in edit mode I get the following exception: <nativehr>0x80020005</nativehr><nativestack></nativestack>Invalid data has been used to update the list item. The field you are trying to update may be read only.
But when my custom field type inherites from SPFieldText I don't get any exception. How to save lookup value in ItemFieldValue correctly when I inherites from SPFieldLookup ?