Matthew
Matthew

Reputation: 4607

ASP.NET - Retrieve image URLs from database and show them in repeater

Basically I have a database with two tables, that is, Updates table and Images table.

The images table is associated with the Updates table through a foreign key "Update_ID".

Now, in my page, I have a repeater which shows all the entries in the Updates table. What I want to do is to display all the images associated with each and every Update_ID in the repeater control.

The problem is that each Update_ID can have as many images as one wants associated with it. If there was only one image per update_ID then there would be no problem as I would allocate one Image control and that's it. However, I don't know how many Image controls I am going to have.

How can I show the images associated with each update_ID in its respective repeater? Thanks :)

Upvotes: 0

Views: 2001

Answers (2)

Amir Ismail
Amir Ismail

Reputation: 3883

Use a Repeater inside each ItemTemplate of the main repeater and Bind the new repeater to the images collection of each update record

<asp:Repeater runat="server" ID="rptrUpdates">
   <ItemTemplate>
           <asp:Repeater runat="server" ID="rptrImages">
             <ItemTemplate>
               <img src="<%#Eval("imgUrl")"/>
             </ItemTempate>
           </asp:Repeater>
   </ItemTempate>
</asp:Repaeter>

and in code behind inside in ItemDataBound event of main repeater bind the child one

protected void rptrUpdates_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
   if(e.Item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
   {
      Repeater rptrImages = (Repeater)e.Item.FindControl("rptrImages");
      rptrImages.DataSource = ((Updates)e.Item.DataItem).Images;
      rptrImages.DataBind();
   }
}

Upvotes: 1

Related Questions