我有一个包含9个项目的转发器,其中的值在客户端页面上分配,所有9个显示完成。
我正在使用ItemDataBound在服务器端编辑项目图像,但它只返回9中的8个。我调试它并且它从不拾取第一个项目。有什么想法吗?
ASPX:
<asp:Repeater ID="rep_FeaturedProds" runat="server" DataSourceID="SqlDataSource1" OnItemDataBound="rep_FeaturedProds_OnItemDataBound">
<ItemTemplate>
<li>
<div class="Homepage_FeaturedProds_HR"></div>
<div class="Homepage_FeaturedProds_HR_Item">
<a href='Product.aspx?pid=<%# DataBinder.Eval(Container.DataItem,"decID") %>'>
<div id="Homepage_FeaturedProds_BG" class="Homepage_FeaturedProds_BG" runat="server">
<div class="Homepage_FeaturedProds_Thumb"><img id="imgProduct_Thumb" runat="server" /></div>
</div>
<div class="Homepage_FeaturedProds_Title"><%# DataBinder.Eval(Container.DataItem,"productDefaultName") %></div>
<div class="Homepage_FeaturedProds_Rating"></div>
<div class="Homepage_FeaturedProds_MoreInfo">More Info ></div>
<asp:HiddenField ID="hfDecId" runat="server" Value='<%# DataBinder.Eval(Container.DataItem,"decID") %>' />
</a>
</div>
</li>
</ItemTemplate>
</asp:Repeater>
C#代码:
protected void rep_FeaturedProds_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
foreach (RepeaterItem ri in rep_FeaturedProds.Items)
{
HtmlImage imgProduct_Thumb = (HtmlImage)e.Item.FindControl("imgProduct_Thumb");
HiddenField hfImageFileName = (HiddenField)e.Item.FindControl("hfDecId");
HtmlGenericControl Homepage_FeaturedProds_BG = (HtmlGenericControl)e.Item.FindControl("Homepage_FeaturedProds_BG");
string imgPath = "~/Uploads/Images/ProductThumbs/";
string decId = hfImageFileName.Value + "_thumb.png";
if (System.IO.File.Exists(Server.MapPath(imgPath + decId)))
{
imgProduct_Thumb.Src = imgPath + decId;
}
else
{
imgProduct_Thumb.Src = imgPath + "0000_thumb.png";
}
}
}
}
答案 0 :(得分:1)
AS @Lincolnk提到,删除以下行修复了该问题。即使在使用ri.findcontrol引用控件以查找时,也存在此问题。通过删除和使用e.Item.FindControl修复。
foreach (RepeaterItem ri in rep_FeaturedProds.Items)