Answered by:
Problem binding repeater control

Question
-
User2103817411 posted
Hi, Im trying to load flickr photo to a repeater control but i keep having an error :
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
Please what is wrong with the code below? thanks
if (!Page.IsPostBack) { Flickr.CacheDisabled = true; string defaultKeyword = "Ibrahim Ado Haruna"; GetPhotos(defaultKeyword); Literal HiddenPhotoId = Repeater1.Items[0].FindControl("HiddenPhotoId") as Literal; Literal HiddenPhotoUrl = Repeater1.Items[0].FindControl("HiddenPhotoUrl") as Literal; string photoId = HiddenPhotoId.Text; string photoUrl = HiddenPhotoUrl.Text; // PreviewImage.ImageUrl = photoUrl; GetDescription(photoId); } string flickrKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; string sharedSecret = "xxxxxxxxxxxxxxxx"; private void GetPhotos(string tag) { PhotoSearchOptions options = new PhotoSearchOptions(); options.PerPage = 4; options.Page = 1; options.SortOrder = PhotoSearchSortOrder.DatePostedDescending; options.MediaType = MediaType.Photos; options.Extras = PhotoSearchExtras.All; options.Tags = tag; Flickr flickr = new Flickr(flickrKey, sharedSecret); PhotoCollection photos = flickr.PhotosSearch(options); Repeater1.DataSource = photos; Repeater1.DataBind(); } private void GetDescription(string photoId) { Flickr flickr = new Flickr(flickrKey, sharedSecret); PhotoInfo info = flickr.PhotosGetInfo(photoId); }
<asp:Repeater ID="Repeater1" runat="server"> <ItemTemplate> <div class="masonry-grid grid-col-4"> <div class="grid-sizer"></div> <div class="grid-item"> <div class="post-image"> <img src='<%# Eval("SquareThumbnailUrl") %>' width="600" height="400" alt="" title="" /> </div> <h2 class="post-title"> <a href="blog-single.html"><%# Eval("Title") %> </a> </h2> <asp:Literal ID="HiddenPhotoId" runat="server" Text='<%# Eval("PhotoId") %>' Visible="false" /> <asp:Literal ID="HiddenPhotoUrl" runat="server" Text='<%# Eval("MediumUrl") %>' Visible="false" /> </div> </div> </ItemTemplate> </asp:Repeater>
Tuesday, August 2, 2016 4:26 PM
Answers
-
User-691209617 posted
After reviewing the code it seems like error can come on these two lines
Literal HiddenPhotoId = Repeater1.Items[0].FindControl("HiddenPhotoId") as Literal; Literal HiddenPhotoUrl = Repeater1.Items[0].FindControl("HiddenPhotoUrl") as Literal;
Repeater might have 0 items and you are trying to get "HiddenPhotoId" Literal Control from first row. You have to write this code in such a way,
string photoId = "0"; //Initialize with 0
string photoUrl = ""; //Initialize with empty string
if(Repeater1.Items.Count > 0) {
photoId = (Repeater1.Items[0].FindControl("HiddenPhotoId") as Literal).Text; photoUrl = (Repeater1.Items[0].FindControl("HiddenPhotoUrl") as Literal).Text;
}- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, August 2, 2016 7:32 PM
All replies
-
User-691209617 posted
After reviewing the code it seems like error can come on these two lines
Literal HiddenPhotoId = Repeater1.Items[0].FindControl("HiddenPhotoId") as Literal; Literal HiddenPhotoUrl = Repeater1.Items[0].FindControl("HiddenPhotoUrl") as Literal;
Repeater might have 0 items and you are trying to get "HiddenPhotoId" Literal Control from first row. You have to write this code in such a way,
string photoId = "0"; //Initialize with 0
string photoUrl = ""; //Initialize with empty string
if(Repeater1.Items.Count > 0) {
photoId = (Repeater1.Items[0].FindControl("HiddenPhotoId") as Literal).Text; photoUrl = (Repeater1.Items[0].FindControl("HiddenPhotoUrl") as Literal).Text;
}- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, August 2, 2016 7:32 PM -
User61956409 posted
Hi ibcent2003,
PhotoCollection photos = flickr.PhotosSearch(options);Repeater1.DataSource = photos;
Please ensure that you populate/bind data source to Repeater control before you find control from Repeater items. So please debug the code to check the value of photos to make sure if it has rows.
Best Regards,
Fei Han
Wednesday, August 3, 2016 3:09 AM -
User-1034726716 posted
codemovement was right. It's always recommended to check for the Rows/Items count (whether you are checking the DataControl or the DataSource itself) before accessing its items.
Also, you might consider accessing the items at ItemDataBound event of Repeater to ensure that the repeater is bounded with data.
Wednesday, August 3, 2016 10:52 AM