hi, i am trying to bind a list of image to the repeater. I have the location of the image in the database saved in the format "~/server/image.jpg". I load the location of the images from the database assign it to an image and put it into an imageList and
bind it to a repeater. But the images doesn't load.
here is the code behind:
protected void Page_Load(object sender, EventArgs e)
{
List<Image> Images = new List<Image>();
DatabaseManager dm = new DatabaseManager();
FID = Convert.ToInt32(Request.QueryString["FID"]);
DataTable dt = new DataTable();
dt = dm.returnPhotosLocation(FID);//returns a list of location of image eg:~/server/image.jpg
foreach (DataRow row in dt.Rows)
foreach (var Item in row.ItemArray)
{
Image img = new Image();
img.ImageUrl = Item.ToString();
Images.Add(img);
}
You can bind the DataTable to Repeater directly and bind the image url field to the ImageUrl property to Image control in Repeater. No need the Images list.
arketes
Member
4 Points
5 Posts
binding List<image> to a repeater
Jun 29, 2012 02:25 PM|LINK
hi, i am trying to bind a list of image to the repeater. I have the location of the image in the database saved in the format "~/server/image.jpg". I load the location of the images from the database assign it to an image and put it into an imageList and bind it to a repeater. But the images doesn't load.
here is the code behind:
protected void Page_Load(object sender, EventArgs e) { List<Image> Images = new List<Image>(); DatabaseManager dm = new DatabaseManager(); FID = Convert.ToInt32(Request.QueryString["FID"]); DataTable dt = new DataTable(); dt = dm.returnPhotosLocation(FID);//returns a list of location of image eg:~/server/image.jpg foreach (DataRow row in dt.Rows) foreach (var Item in row.ItemArray) { Image img = new Image(); img.ImageUrl = Item.ToString(); Images.Add(img); }imagesRepeater.DataSource = Images; imagesRepeater.DataBind();}here is the aspx part:
<asp:Repeater ID = "imagesRepeater" runat = "server">
<ItemTemplate>
<asp:Image ID = "images" runat = "server" ImageUrl = '<%# Container.DataItem %>' />
</ItemTemplate>
</asp:Repeater>
What am i doing wrong??
Repeater
Mikesdotnett...
All-Star
154927 Points
19867 Posts
Moderator
MVP
Re: binding List<image> to a repeater
Jun 29, 2012 02:39 PM|LINK
You should databind after the foreach loops have finished:
foreach (DataRow row in dt.Rows) foreach (var Item in row.ItemArray){ Image img = new Image(); img.ImageUrl = Item.ToString(); Images.Add(img); } } imagesRepeater.DataSource = Images; imagesRepeater.DataBind();Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
arketes
Member
4 Points
5 Posts
Re: binding List<image> to a repeater
Jun 29, 2012 03:54 PM|LINK
thanks for pointing that out. i corrected it, but still no luck. The image wont load!!
Mikesdotnett...
All-Star
154927 Points
19867 Posts
Moderator
MVP
Re: binding List<image> to a repeater
Jun 29, 2012 05:11 PM|LINK
Check the HTML source of the page to ensure the url of the image file is correct.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
arketes
Member
4 Points
5 Posts
Re: binding List<image> to a repeater
Jun 29, 2012 09:20 PM|LINK
the url of the image file is correct. If i take a single url and assign it to ImageUrl of an image which is already in the designer, it works fine.
What i am saying is, if i do:
image1.ImageUrl = item.Tostring(); //image1 is an image control already in the designer
this is working.
Qin Dian Tan...
All-Star
113532 Points
12480 Posts
Microsoft
Re: binding List<image> to a repeater
Jul 03, 2012 08:07 AM|LINK
Hi,
You can bind the DataTable to Repeater directly and bind the image url field to the ImageUrl property to Image control in Repeater. No need the Images list.
Thanks,
If you have any feedback about my replies, please contactmsdnmg@microsoft.com.
Microsoft One Code Framework