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;
}
}
答案 0 :(得分:1)
对于转发器中的每个项目,您具有相同链接的原因是,当您将项目绑定到转发器时,每个项目都会运行ItemDataBound事件。在该事件中,您遍历每个Product项并在foreach循环中反复覆盖HyperLink NavigateURL,直到列表中的LAST产品是超链接的剩余值。
如果设置断点并逐步执行ItemDataBound事件,您将看到此行为。
我无法告诉您如何修复它,因为我根本看不到您打算如何将文件映射到产品。一旦你这样做,我建议将你的转发器绑定到产品列表,并将一个公共属性添加到你的产品模型,它将保存你想要显示的图像的文件位置。然后可以在ItemDataBound事件中或在aspx标记中设置,就像现在使用src一样。
有关ItemDataBoundEvent的一些信息,包括如何获取基础数据项。
答案 1 :(得分:-1)
protected void Page_Load(object sender, EventArgs e)
{
FillPage();
if (!IsPostBack)
{
GetData(null, null, null, null);
}
DbRetrival();
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
string ID = DataBinder.Eval(e.Item.DataItem, "ID").ToString();
HyperLink hp = (HyperLink)e.Item.FindControl("link");
hp.NavigateUrl = "~/pages/Product.aspx?id=" + ID ;
}
private void DbRetrival()
{
string cs = ConfigurationManager.ConnectionStrings["SuperStoreDBConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(cs);
DataTable dt = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter("Select ID,Image,Name from Product",con );
adp.Fill(dt);
Repeater1.DataSource = dt;
Repeater1.DataBind();
}
private void FolderRetrival()
{
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();
}
asp页面:
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
<li>
<asp:HyperLink ID="link" runat="server">
<br /> <asp:Image runat="server" ImageUrl='<%#"~/pages/Management/Images/Products/" + String.Format("{0}",
Eval("Image")) %>' Width='180px' Height='145px' /> </Image>
<br /> <%# Eval("Name")%>
</asp:HyperLink>
</li>
</ItemTemplate>