[英]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. 基本上我有一个包含两个表的数据库,即Updates表和Images表。
The images table is associated with the Updates table through a foreign key "Update_ID". images表通过外键“Update_ID”与Updates表相关联。
Now, in my page, I have a repeater which shows all the entries in the Updates table. 现在,在我的页面中,我有一个转发器,显示Updates表中的所有条目。 What I want to do is to display all the images associated with each and every Update_ID in the repeater control.
我想要做的是显示与转发器控件中的每个Update_ID相关联的所有图像。
The problem is that each Update_ID can have as many images as one wants associated with it. 问题是每个Update_ID可以拥有与想要关联的图像一样多的图像。 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.
如果每个update_ID只有一个图像,那么就没有问题,因为我会分配一个Image控件,就是这样。 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? 如何在各自的转发器中显示与每个update_ID关联的图像? Thanks :)
谢谢 :)
Use a Repeater
inside each ItemTemplate
of the main repeater and Bind the new repeater to the images collection of each update record 在主转发器的每个
ItemTemplate
中使用Repeater
,并将新转发器绑定到每个更新记录的images集合中
<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 并且在主要转发器的
ItemDataBound
事件内部的代码中绑定了一个子代码
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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.