Search code examples
c#sitecoretreelist

Loading multiple images from a treelist in sitecore


I'm getting started with Sitecore and am having a hard time getting images stored in a TreeList to display properly in an aspnet repeater.

In the markup, I have a repeater that is meant to list the images using an sc:Image item.

                <asp:Repeater ID="rptLogos" runat="server" OnItemDataBound="rptLogos_ItemDataBound" >
                    <ItemTemplate>
                        <a title="Logo" href="#">
                            <sc:Image runat="server" ID="img" field="image logos" />
                        </a>
                    </ItemTemplate>
                </asp:Repeater>

In the code behind, I'm retrieving a MultilistField and setting the repeater's data source with the TargetIDs like so

        Sitecore.Data.Fields.MultilistField mlLogos = Footer.Fields["Logos"];

        if (mlPartnerLogos != null)
        {
            rptLogos.DataSource = mlLogos.TargetIDs;
            rptLogos.DataBind();
        }

In the ItemDataBound event I am then converting each ID to an item, grabbing the image control from the repeater and setting the Item field for the Sitecore.Web.UI.WebControls.Image item.

        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            ID targetId = e.Item.DataItem as ID;

            if (!targetId.IsNull)
            {
                MediaItem targetItem = Sitecore.Context.Database.GetItem(targetId);

                if (targetItem != null)
                {
                    var phImage = e.Item.FindControl("img") as Sitecore.Web.UI.WebControls.Image;
                    phImage.Item = targetItem;
                }
            }
        }

If I debug the code, it seems to be retrieving the data without any issues. However, in the markup, instead of having the translated to an field, I'm just getting blank text.

The same "idea" of grabbing a Sitecore Item and assigning it to a Sitecore.Web.UI.WebControls.Image item is working fine elsewhere where I have a single item, although I did notice that if I change the sc:Image tag's field to anything other than the name in lowercase of the field name in sitecore it won't work. If this is the issue though, I'm a bit confused as to what the Field name should be for the images within the TreeList though.

I also have a strange feeling that the way I'm retrieving the items and trying to assign them may be a bit too "bloated", and there may be a simpler way of doing it.

Any help would be greatly appreciated. Thank you.

Edit:

Fixed as suggested by replacing the sc:image with an asp:Image and changing the ItemDataBound to

        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            ID targetId = e.Item.DataItem as ID;

            if (!targetId.IsNull)
            {
                MediaItem targetItem = Sitecore.Context.Database.GetItem(targetId);

                if (targetItem != null)
                {
                    var phImage = e.Item.FindControl("img") as System.Web.UI.WebControls.Image;
                    phImage.ImageUrl = Sitecore.Resources.Media.MediaManager.GetMediaUrl(targetItem);
                }
            }
        }

Solution

  • The sc:image control is to be used to display fields of an item. I think it's a better approach to just use an asp:image control and fill the image url. If you use the MediaManager you can get the image url from your image item.