I am doing custom visual web part(Sandboxed) developement. I am facing a little hurdle as I unable to retrieve Title, Image and URL columns from my custom list and bind it to a repeater control. Also how to bind the URL to Image on click.My CAML query selects values based on another column if its value is less than 5. Please provide your suggestions.I am just beginning with SP2010 Development, so really appreciate your help. Cheers :)

My Ascx.cs code

                      SPList list = web.Lists[listName.ToString()];
                            //Getting the latest folder in the list
                            string serverPath = web.Url.ToString();
                            SPQuery query = new SPQuery();
                           query.Query  = @"<Where><Leq><FieldRef Name='ItemOrder' /><Value Type='Number'>5</Value></Leq></Where><OrderBy><FieldRef Name='ItemOrder' Ascending='True' /></OrderBy>";
                           query.RowLimit = 5;
                           SPListItemCollection itemColl = list.GetItems(query);
                           bool allowUpdates = web.AllowUnsafeUpdates; //store original value 
                           web.AllowUnsafeUpdates = true; 
                           if (itemColl.Count > 0)
                               foreach (SPListItem item in itemColl)
                                   rptTitle.DataSource = itemColl.Fields["Title"];

                                   rptImage.DataSource = itemColl.Fields["Image"];
                                   //hplViewAll.NavigateUrl = web.ServerRelativeUrl;

                               UserFriendly("No items to Display");
                           web.AllowUnsafeUpdates = allowUpdates;

I am sorry, my bad. I am trying to display both Image and title from my custom list. I have two repeaters for which I would have to map my source. MY ASCX CODE

<%# ((Carousel)Container.DataItem).Title %> " alt=""/>

1 Answer 1


You are not fetching item values properly. In your foreach, 'item["Title"]' holds your values. itemColl.Fields["Title"] returns the Field itself (SPField), not the value.

Your web.AllowUnsafeUpdates serves no purpose here, remove it. It's only useful if you want to update items in a GET web request. See technical article on this property.

SPWeb.Url is already a string, no need to call ToString() on it.

You should create an independent entity class and bind to that. SPListItem[ string ] is not suitable to be a datasource for a Repeater, which requires at least IEnumerable or IListSource interface on the bound object. Furthermore, you would expose yourself to SPDispose leaks as this would certainly reopen your SPWeb object internally and not close them properly. Binding to simple entity class or non-complex objects is the recommended practice.

public class MyItemData {
    string Title { get; set; }
    string Image { get; set; }

Fill and bind to a List< MyItemData> in your OnLoad. Your Repeater should do the rest.

var myItems = new List< MyItemData>();
foreach (SPListItem item in itemColl)
    myItems.Add(new MyItemData() { Title = item["Title"], Image = item["Image"] });

Once that is done, simply bind your datasource to List< MyItemData>. IList is a suitable object to serve as a Repeater datasource :

rptTitle.DataSource = myItems;

Alternatively, you can merge your two repeaters together as they should be showing the same number of items anyways. And simply display values as you were before.

  • Thanks for your reply Louis. Sorry as I told before I am beginner, so would really appreciate to clarify the below. 1) May I know why we had to create an independent entity class? rather can i bind item["title"] and item["Image"] directly to the repeater? 2) List<MyItemData> myItems will be bound with column Title and Image, but how do I hook these to repeater datasource? foreach (SPListItem item in itemColl) { myItems.Add(new MyItemData() { Title = item["Title"], Image = item["Image"] }); } Thanks! Aug 5, 2012 at 11:44
  • Edited with some more details after your comment
    – Louis
    Aug 6, 2012 at 11:46
  • I am actually trying to bind two different columns to two different repeater ? Below is my ascx code! Really Appreciate your help! <asp:Repeater ID="rptTitle" runat="server"> <ItemTemplate> <li> <a href=""> <%# ((Carousel)Container.DataItem).Title %> </a></li> </ItemTemplate> </asp:Repeater> </ul> <ul id="output"> <asp:Repeater ID="rptImage" runat="server"> <ItemTemplate> <li><img src="<%# Container.DataItem %>" alt=""/></li> </ItemTemplate> </asp:Repeater> Aug 7, 2012 at 3:31
  • The only issue with this is for some reason, I am unable to retrieve the images. I use rptImage.DataSource = myItems.Select(itemvalue => itemvalue.ImageUrl); I am binding item.GetFormattedValue("Image"); this value to Image property of MyItemData class.Guess I am missing something down here? Aug 7, 2012 at 5:02
  • I had format the URL and it resolved it. Thanks for sharing all the information, Appreciate it! :) Aug 7, 2012 at 11:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.