Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am working with some custom fields. One of them is a choice field. I need to be able to get the value of the currently selected item in the choice field rendering control (i.e. what would be FieldRenderingControl.Value) in the other custom field rendering control.

I know I could probably wrangle some jQuery to hook into the drop-down and get the value every time it changes (although I took a quick stab at that already and was having problems), but I'm OK with having the first field post-back every time because the choices in the second field are going to change depending on the currently selected item in the first field, so a visit to the server is fine.

My current approach is the following - in the OnAdded() method of the second field, I look for the first field and set it as the SPField.RelatedField by doing this:

public override void OnAdded(SPAddFieldOptions op)
{
    base.OnAdded(op);
    try
    {
        this.RelatedField = this.ParentList.Fields.Cast<SPField>().Where(fld => fld.TypeAsString.Equals("MyFirstCustomFieldType")).Single().InternalName;
        this.Update();
    }
    catch (Exception ex)
    {
        // log the error
    }
}

And then in the field rendering control for the second field, I try to get the value from the first field like this:

SPField relatedField = this.List.Fields.GetFieldByInternalName(this.Field.RelatedField);

string selectedValue = this.ListItem.Fields[relatedField.Id].FieldRenderingControl.Value.ToString();

This is not working. Of particular note is that, following certain patterns I found online (I'm new to custom fields), the get of the FieldRenderingControl property of the first field returns a new field rendering control object, so it's not ever going to find the value of the current field rendering control.

I have also speculated that, if I stored the field ID or Display Name in the RelatedField property (instead of the Internal Name), I could get the value in the database of the first field by using this.ListItem[this.RelatedField] (casting as GUID if I used ID, of course), however this doesn't help me if it's a new item (with no value in the database), or if the selected item changes in the drop-down but hasn't been submitted yet (and thus hasn't been written to the database yet).

So... any insight as to how I can get the currently selected value of a field rendering control in a different field rendering control, server-side?

EDIT: Ok, so I solved the issues I was having with jQuery and can manage to get the value of the first field rendering control into the second field rendering control while staying client-side, but I'm not sure I like this approach as it makes the two rendering controls too tightly coupled. Still would like to know if there's a way to do this server-side.

share|improve this question
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.