views:

813

answers:

1

I am trying to make a grid of thumbnails using a datalist. I have an array of the imageurls and I want to bind them in the code behind. How would I go about do that? I want the datalist to have a max column size of 5 and add rows of thumbnails until completed.

<asp:DataList ID="dlImages" runat="server" 
          RepeatColumns="5" 
          RepeatDirection="Horizontal" 
          RepeatLayout="Flow">

   <ItemTemplate>
      <asp:Image ID="imgStore" runat="server" />
   </ItemTemplate>

</asp:DataList>

CODEBEHIND:

protected void BindImages(string[] imageurls)
{
    for (int i = 0; i < imageurls.Length; i++)
    {
        .
        .
        .
    }
}
+5  A: 

I think this will do it for you

<asp:DataList ID="dlImages" runat="server" 
      RepeatColumns="5" 
      RepeatDirection="Horizontal" 
      RepeatLayout="Flow">

<ItemTemplate>
   <img src="<%# (string)Container.DataItem %>" />
</ItemTemplate>

</asp:DataList>

Code behind

protected void BindImages(string[] imageurls)
{
    dlImages.DataSource = imageurls; 
    dlImages.DataBind();
}

You might also be able to do

<asp:Image runat="server" id="imgStore" 
     ImageUrl="<%# (string)Container.DataItem %>" />

But sometimes server controls don't like render blocks in them.

Bob
This works great if I use actual imageurls, but if I use ones that I created for example: ~/UserPages/Photo/GetThumbnail.aspx?id=7...It doesn't work.
Is there a reason for that?