How do I render a Media Library item by itself?

时光毁灭记忆、已成空白 提交于 2019-12-07 00:15:03

问题


How do I render a Media Library item image in a sublayout with Sitecore controls? With a normal content Item that has an "Image" field it's a piece of cake... just use the sc:Image or FieldRenderer control. But I have something like this:

<asp:Repeater ID="rptImages" runat="server">
  <ItemTemplate>
    <sc:FieldRenderer  ????>
  </ItemTemplate>
</asp:Repater>

And the code behind:

rptImages.DataSource = Sitecore.Context.Database.SelectItems("/sitecore/Media Library/Images/Some Image Folder/*")
rptImages.DataBind();

So... what goes in the ItemTemplate? I'm OK with using the ItemDataBound event to do some manual assignment, but it's unclear to me how to go about that. I feel like I'm missing something really simple here.


回答1:


OK... I had trouble answering this originally because I was thinking about it from the wrong angle. The key is NOT using Sitecore controls in this scenario. What you really need to do is just get the URL from the Sitecore API and use standard HTML controls:

<asp:Repeater ID="rptImages" runat="server" OnItemCommand="rptImages_ItemCommand">
    <ItemTemplate>
        <img src='<%# Sitecore.StringUtil.EnsurePrefix('/', Sitecore.Resources.Media.MediaManager.GetMediaUrl((Sitecore.Data.Items.Item)Container.DataItem)) %>' />
    </ItemTemplate>
</asp:Repeater>



回答2:


I'm aware it's an old question, however:

You could use the following:

<asp:Repeater ID="rptImages" runat="server">
    <ItemTemplate>
        <sc:FieldRenderer ID="image" runat="server" FieldName="FieldName" 
              Item="<%# Container.DataItem %>
    </ItemTemplate>
</asp:Repeater>

Or replace the FieldRenderer with the sc:Image.




回答3:


I've been wondering the same thing, and I've realized that there's one big difference: FieldRenderer (and sc:Image) are FieldRenderers... A MediaItem is not a field, but an item. That's where the problem is: we should use Image fields, or as the accepted answer states: use plain html.



来源:https://stackoverflow.com/questions/3835050/how-do-i-render-a-media-library-item-by-itself

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