Liberty Crown Infotech Liberty Crown Infotech - 13 days ago 7
ASP.NET (C#) Question

Why am I getting the same Image id from different Image view in a repeater?

I have a repeater that slide shows images from a folder, such that when you click on the current image in the slide, a product page is opened that shows the details information of the selected image.
Meanwhile, I have a productModel that list all the images from the database, such that when you click on any of the images it opens the image product page as above.
Now, my concern is that the hyperlink I attached to the repeater keeps opening the same product page from different image that appears on the repeater. Could anyone please help me point out what I am doing wrong? I will be most grateful!

<ul class="bjqs">
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate><li>
<asp:HyperLink ID="link" runat="server">
<img src='<%# DataBinder.Eval(Container.DataItem,"Value") %>'
title='<%# (DataBinder.Eval(Container.DataItem,"Text").ToString()).Split('.')[0].ToString() %>' alt="">
</asp:HyperLink></li>
</ItemTemplate>
</asp:Repeater>
</ul>


The Server side code:

protected void Page_Load(object sender, EventArgs e)
{
FillPage();
string[] filePaths = Directory.GetFiles(Server.MapPath("~/pages/Management/Images/Products/"));
List<ListItem> files = new List<ListItem>();
foreach (string filePath in filePaths)
{
string fileName = Path.GetFileName(filePath);
files.Add(new ListItem(fileName, "/pages/Management/Images/Products/" + fileName));
}
Repeater1.DataSource = files;
Repeater1.DataBind();
}
}

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{

ProductModel productModel = new ProductModel();
List<Product> products = productModel.GetAllProducts();

foreach (Product product in products)
{
Panel productPanel = new Panel();
HyperLink hp = (HyperLink)e.Item.FindControl("link");
hp.NavigateUrl = "~/pages/Product.aspx?id=" + product.ID;
}
}

Answer

The reason you have the same Link for every item in your repeater is that when you bind an item to the repeater every single item runs the ItemDataBound event. Inside that event you loop through every Product item and overwrite the HyperLink NavigateURL over and over in the foreach loop until the LAST product in the list is the remaining value for the hyperlink.

If you set a breakpoint and step through the ItemDataBound event you will see this behavior.

I cannot tell you how to fix it because I simply do not see how you intend to map a file to a Product. Once you do that I would suggest bind your repeater to the Product list and add a public property to your Product model which will hold the file location of the image you wish to show. This could then be set in the ItemDataBound event or in the aspx markup as you are doing now with the src for instance.

Some info on the ItemDataBoundEvent including how to get the underlying data item.

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemdatabound(v=vs.110).aspx

Comments