为什么我在转发器中从不同的图像视图中获取相同的图像ID? [英] Why am I getting the same image id from different image view in a repeater?

查看:63
本文介绍了为什么我在转发器中从不同的图像视图中获取相同的图像ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个转发器,幻灯片显示文件夹中的图像,这样当您单击幻灯片中的当前图像时,将打开一个产品页面,显示所选图像的详细信息。同时,我有一个productModel列出了数据库中的所有图像,这样当你点击任何图像时,它会打开上面的图像产品页面。现在,我担心的是我附加到转发器的超链接会从转发器上显示的不同图像打开相同的产品页面。谁能帮助我指出我做错了什么?我将非常感激!



我的尝试:



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:

<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:

<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>

推荐答案

正如评论中所述,每次在转发器中创建一个项目(Repeater1_ItemDataBound)你循环遍历所有产品并设置超链接NavigateURL。因此,它将针对每个项目运行。



您可能需要if语句才能找到与您所在行匹配的正确产品。
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.


这篇关于为什么我在转发器中从不同的图像视图中获取相同的图像ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆