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

三世轮回 提交于 2019-11-28 09:27:54

问题


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

<sc:FieldRenderer ID="frTitle" runat="server" 
    FieldName="Title" 
    Item="<%# (Sitecore.Data.Items.Item)Container.DataItem %>" />

but how to access drop-down list field type selected value defined in item.

Thanks


回答1:


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>



来源:https://stackoverflow.com/questions/24633156/how-to-access-drop-down-list-field-type-selected-value-in-sitecore

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!