문제

As stated on MSDN, the CHOICE element contains the value within its body.

That means that in a field, if I have:

<CHOICES>
    <CHOICE>foo</CHOICE>
    <CHOICE>bar</CHOICE>
</CHOICES>

the HTML output is:

<option value="foo">foo</option>
<option value="bar">bar</option>

This is a problem for me as I need the value to be different than the label. What I need is this:

<option value="CODE1">foo</option>
<option value="CODE2">bar</option>

I also tried this:

<CHOICES>
    <CHOICE Value="CODE1">foo</CHOICE>
    <CHOICE Value="CODE2">bar</CHOICE>
</CHOICES>

But the result is still this:

<option value="foo">foo</option>
<option value="bar">bar</option>

Is there a way to specify different value than the label in a field of type Choice?

도움이 되었습니까?

해결책

SPFieldChoice has a property named FieldRenderingControl. Using reflector you can find this code in the FieldRedneringControl property:

....
switch (this.EditFormat)
{
    case SPChoiceFormatType.Dropdown:
        control = new DropDownChoiceField();
        break;

    case SPChoiceFormatType.RadioButtons:
        control = new RadioButtonChoiceField();
        break;

    default:
        return null;
}
....

It seems that DropDownChoiceField is our control. Navigate to this control and in CreateChildControls you can see:

....
this.m_dropList.DataSource = this.DataSource;
this.m_dropList.DataValueField = "ValueField";
this.m_dropList.DataTextField = "TextField";
this.m_dropList.DataBind();
....

..lets go to DataSource property:

....
    DataTable table = new DataTable();
    table.Locale = CultureInfo.InvariantCulture;
    table.Columns.Add(new DataColumn("ValueField", typeof(string)));
    table.Columns.Add(new DataColumn("TextField", typeof(string)));
    if (string.IsNullOrEmpty((string) this.ItemFieldValue))
    {
        DataRow row = table.NewRow();
        row[0] = string.Empty;
        row[1] = string.Empty;
        table.Rows.Add(row);
        this.m_bHasBlankChoice = true;
    }
    for (int i = 0; i < base.NumberOfChoices; i++)
    {
        DataRow row2 = table.NewRow();
        row2[0] = ((SPFieldChoice) base.Field).Choices[i];
        row2[1] = row2[0];
        table.Rows.Add(row2);
    }
    this.m_dataSource = new DataView(table);
}
return this.m_dataSource;

row2[1] = row2[0] means that value attribute always equals to option's label and your experiments prove it (my answer is related to 2010 sharepoint)

If this behavior extremely needed, may be custom field type will be an option or any workarounds.

다른 팁

There is an explanation how to fix your problem, but I'm not sure that this solution is good, because you should change sharepoint fiels. I think that the best way is to change logic of your application.

Short answer: No.

The only (out of the box) ways to do this are:

  • Lookup field

    • (PRO: behaves as a choice field when rendered)
    • (CON: only works within 1 site only)
  • Managed metadata, adding the value as a synonym

    • (PRO: works everywhere)
    • (CON: is type ahead, so looks different from choice field)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 sharepoint.stackexchange
scroll top