简体   繁体   中英

Repeater Skipping First Row on ItemDataBound

I have a repeater with 9 items where the values are assigned on the client side page and all 9 show complete.

I am editing the item images on the server side with ItemDataBound, but it only returns 8 of the 9. I debugged it and it never picks up the first item. Any ideas?

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

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

            }
        }
    }
}

AS @Lincolnk mentioned, removing the following line fixed the issue. Even when reference the control to find with ri.findcontrol, this issue was present. Fixed by removing and using e.Item.FindControl.

foreach (RepeaterItem ri in rep_FeaturedProds.Items)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM