SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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=""/>

share|improve this question
up vote 1 down vote accepted

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.

share|improve this answer
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! – Battlesmith Aug 5 '12 at 11:44
Edited with some more details after your comment – Louis Aug 6 '12 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> – Battlesmith Aug 7 '12 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? – Battlesmith Aug 7 '12 at 5:02
I had format the URL and it resolved it. Thanks for sharing all the information, Appreciate it! :) – Battlesmith Aug 7 '12 at 11:49

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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