Question

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++)
    {
        .
        .
        .
    }
}
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top