2

I have the following List:

private List<System.Web.UI.WebControls.Image> _searchResultList = new List<System.Web.UI.WebControls.Image>();

This List may contain several Images with different URLs.

I have the following Repeater:

<asp:Panel ID="SearchPanel" runat="server" ScrollBars="Vertical">
    <asp:Repeater ID="Repeater" runat="server">
        <ItemTemplate>
            <asp:Image height="32" width="32" runat="server"/>
        </ItemTemplate>
    </asp:Repeater>
</asp:Panel>

Using DataSource to display the images doesn't seem to work.

Repeater.DataSource = _searchResultList;           
Repeater.DataBind();

What am I doing wrong?

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
Kohnarik
  • 377
  • 2
  • 5
  • 19
  • Similar to this: http://stackoverflow.com/questions/33597934/select-all-images-using-asp-net-c-sharp – Salah Akbari Jan 11 '16 at 12:10
  • Have you checked http://stackoverflow.com/questions/7885444/how-to-show-image-in-the-column-of-repeater-control-in-asp-net ? – zkanoca Jan 11 '16 at 12:10
  • Do you have these images serialized in files somewhere on the server side? The way you display images on a web page is that you have them as files, and you reference them on the web page by providing source information to Image tags. There is no easy way to render just an Image object – Andrei Jan 11 '16 at 12:13
  • @Andrei The standard path to the images is "~/Images/ORAS/". If I create a normal Image on the aspx page I can add an Image by using ImageUrl, but it doesn't work if I have to create them in code-behind. – Kohnarik Jan 11 '16 at 12:19
  • 1
    @Kohnarik...The `_searchResultList` is not a list of strings so you can't use `ImageURL='<%Container.DataItem.ToString()%>'`. Because `_searchResultList` is a list of images you should bind the `ImageUrl` property. This should works fine for you: ` ` – Salah Akbari Jan 11 '16 at 13:19
  • @user2946329 I did that before and it didn't work, but now it does. I have no idea what changed. Nonetheless, thank you so much! – Kohnarik Jan 11 '16 at 13:22
  • @user2946329 Thank you. – Kohnarik Jan 11 '16 at 13:23

2 Answers2

2

The _searchResultList is not a list of strings so you can't use ImageURL='<%Container.DataItem.ToString()%>'. Because _searchResultList is a list of images you should bind the ImageUrl property. This should works fine for you:

<asp:Repeater ID="Repeater" runat="server">
    <ItemTemplate> 
       <asp:Image ID="Image1" height="32" width="32" runat="server" ImageUrl='<%# Eval("ImageUrl") %>' /> 
    </ItemTemplate>
</asp:Repeater>

In this example Container.DataItem refers to an Image control. This is why we used Eval("ImageUrl") to get the ImageUrl property of each Image control.

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
  • 1
    In this answer http://stackoverflow.com/a/33694399/2946329 that I shown you earlier you could use `ImageURL='<%Container.DataItem.ToString()%>'` because the source is a List of string. But in this example `Container.DataItem` refers to an `Image` control. This is why we used `Eval("ImageUrl")` to get the `ImageUrl` of each image control. – Salah Akbari Jan 11 '16 at 13:38
  • I didn't realize that. That's very helpful information. Thank you. – Kohnarik Jan 11 '16 at 13:44
  • Ohh I see. Sorry, I'm kind of brain-afk today. I upvoted it – Kohnarik Jan 11 '16 at 13:48
1
        <asp:Panel ID="SearchPanel" runat="server" crollBars="Vertical">
        <asp:Repeater ID="Repeater" runat="server">
         <ItemTemplate>
            <asp:Image height="32" width="32" runat="server" ImageURL='<%Container.DataItem.ToString()%>'/>// changes here
        </ ItemTemplate>
           </asp:Repeater>
            </asp:Panel>
Sujit.Warrier
  • 2,815
  • 2
  • 28
  • 47
  • As much as I'd like this to work, it doesn't do anything for me. I've checked the _searchResultList and the Images are all in there, like they should. If I add an asp:Image element outside the Repeater and use the same ImageURL, it works. But inside the Repeater, even if I use a static ImageURL, it doesn't show up. – Kohnarik Jan 11 '16 at 12:38
  • Okay, if I change the ImageURL inside the Repeater to ImageURL="~/Images/ORAS/006.png" and there are 17 images in my _searchResultList, the Image 006.png will show up 17 times. – Kohnarik Jan 11 '16 at 12:45
  • give me an example of the url in the list – Sujit.Warrier Jan 11 '16 at 12:46
  • ~/Images/ORAS/006-mx.png – Kohnarik Jan 11 '16 at 12:49
  • hey remove databinder.eval just use <%Container.DataItem.ToString()%> ImageUrl='<%Container.DataItem.ToString()%> – Sujit.Warrier Jan 11 '16 at 12:57
  • That doesn't work either. Although it does seem to recognize the correct amount of pictures, it does not show them. If I highlight the whole page, I can see the correct amount of [squares](http://imgur.com/0SXzFTU) where the pictures are supposed to be at. Without selection: [Normal](http://imgur.com/C6tjBK1) – Kohnarik Jan 11 '16 at 13:05
  • It means the path is not proper. first try removing the ~ sign before the path, also check if the images are present in the path provided.coz this worked perfectly for me – Sujit.Warrier Jan 11 '16 at 13:07
  • right click use inspect elements to check what path the images are getting – Sujit.Warrier Jan 11 '16 at 13:09
  • Instead of ~, I used the exact path, but no change. The path is correct. I even tried adding an ImageURL with Visual Studio's "Choose URL" feature. Creating an image outside the Panel & Repeater works like a charm: – Kohnarik Jan 11 '16 at 13:14
  • right click on the images and check what path is coming (Inspect element) – Sujit.Warrier Jan 11 '16 at 13:15
  • Images/ORAS/004.png, but removing ~/ before the images to achieve the shown path does not work either. – Kohnarik Jan 11 '16 at 13:19
  • Using resolved the issue. Thank you very much for your patience, help and time! – Kohnarik Jan 11 '16 at 13:24