Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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!

What I have tried:

ASP.NET
<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>

code behind:

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

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{

        ProductModel productModel = new ProductModel();
    List&lt;Product&gt; products = productModel.GetAllProducts();

    foreach (Product product in products)
    {
        Panel productPanel = new Panel();        
        HyperLink hp = (HyperLink)e.Item.FindControl(&quot;link&quot;);
        hp.NavigateUrl = &quot;~/pages/Product.aspx?id=&quot; + product.ID;
    }
}</pre>
Posted
Updated 17-Nov-16 7:47am
Comments
ZurdoDev 16-Nov-16 15:53pm    
Why are you foreaching on products in the ItemDataBound event? ItemDataBound fires for each item in your repeater. You need to set the hyperlink one time, not in a foreach loop.

1 solution

As mentioned in comments, each time an item is created in your repeater (Repeater1_ItemDataBound) you are looping through all of your products and setting the hyperlinks NavigateURL. So, it will run that for every item.

You likely need an if statement to find the right product to match the row you are on.
 
Share this answer
 
Comments
Liberty Crown Infotech 17-Nov-16 13:13pm    
Thanks for the attempted solution Ryan. Forgive my naivety, I am actually not a Pro on repeaters. I have remove the for each and it is helpful. Now each time I click on the repeater Image I am getting the same url " http://localhost:1051/pages/Product.aspx?id= ". How do I pass id int value into the url depending on the current image from the folder, using the if statement you suggested above?
ZurdoDev 17-Nov-16 13:17pm    
There are many ways to do it, one way would be to add a label that has the product id and use Eval to set the value. Then in itemdatabound event find the control and set the url of the hyperlink.

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.onitemdatabound(v=vs.110).aspx
Liberty Crown Infotech 17-Nov-16 13:42pm    
Thanks for the attempted solution RyanDev. I am sorry for my naivety, I am not a pro on Repeaters. I have remove the foreach base on your advice and it helps. However, I keep getting the same url when clicked on any current Image without any product id value being passed "http://localhost:1051/pages/Product.aspx?id=" Can you give me head up on how to use the if statement to find the right products from folder to match the row i am on?
ZurdoDev 17-Nov-16 14:00pm    
Do what I suggested in the previous comment.
Liberty Crown Infotech 19-Nov-16 6:01am    
Please,I dont seem to know how to achieve that, I am having a challenge of mapping each file to a Product in the Product Model.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900