How to access drop down list field type selected value in sitecore

后端 未结 1 735
灰色年华
灰色年华 2020-12-21 18:26

I can access Single-Line text field type by following in Repeater:



        
相关标签:
1条回答
  • 2020-12-21 18:40

    This depends on the exact field type you are using.

    If using a Droplist field type

    The value is stored in Sitecore as plain text representing the name* of the selected item. In this case you could use your code sample to render out the name of the selected item (if that's really what you want to do). *Note that the content editor will see the Display Name of the items in the droplist, but your code would render out the item name. Using this field type is not usually a good idea as it is not possible to translate item names.

    If using a DropTree or Droplink field type

    The value is stored in Sitecore is an ID of the linked item. In this case you would need to get the selected item using this ID, then render out the required field of that item. I would probably write a code-behind method to get the selected item, then call this method within your FieldRenderer. Something like this:

    Code-behind:

    public Item GetLinkedItem(Item item, string itemField)
    {
        string dropDownItemId = item[itemField];
        return Sitecore.Context.Database.GetItem(dropDownItemId);
    }
    

    Ascx markup:

    <asp:Repeater ID="rptChildren" runat="server">
        <ItemTemplate>
            <sc:FieldRenderer ID="frTitle" runat="server"
                FieldName="Title"
                Item='<%# GetLinkedItem((Sitecore.Data.Items.Item)Container.DataItem, "YourDropLinkFieldName") %>' />
    </ItemTemplate>
    

    0 讨论(0)
提交回复
热议问题