views:

61

answers:

1

I have an ASP.NET web app that places several string data into an object as properties. The string data is sourced from a JSON feed from Twitter. A collection of my TwitterMessages is held in a Generic List.

What I want to do is use an asp:repeater to iterate through my List<T> to display all of the contents of the objects it holds, by using <%# Eval("MyURL") %>. MyURL is the name of a property inside my TwitterMessage object.

In a previous question I was advised to use the asp:Repeater template to display my data in a custom HTML table. So being able to use a template somehow or other, is really what I'm interested in.

Where I am struggling is in working out how to perform a databind to the Repeater so I can reference my object data in the .aspx page.

I am aware that I need to use the ItemDataBound method in order to make a data-bound event so that I can reference the property names in my string data object.

Hope that's enough info for some much appreciated help! :)

+2  A: 

It's pretty straightforward to databind to your repeater. Depending on where your list object resides, you can either bind your repeater using markup, or codebehind.

If you want to do it in markup, you should make an ObjectDataSource wrapper around the List. Something like this:

<asp:Repeater ID="rpt" runat="server" DataSourceID="twitterFeedSource" >
<ItemTemplate>
<tr><td><%# Eval("MyURL") %></td></tr>
</ItemTemplate>
</asp:Repeater>

<asp:ObjectDataSource ID="twitterFeedSource" runat="server"
  SelectMethod="GetTheListOfTwitterFeedObjects"
  TypeName="myTwitterFeedClass"
>
</asp:ObjectDataSource>

The GetTheListOfTwitterFeedObjects method should return the list of objects. Each object would need to have the property MyURL. You can of course extend your template and bind to any other properties that your twitter objects have.

Otherwise, you can do it straight from the code. Simply do something like this in Page_Load:

if (!IsPostBack)
{
    myRepeater.DataSource = myGenericList;
    myRepeater.DataBind();
}
womp
'The DataSourceID of 'rpt' must be the ID of a control of type IDataSource' is an error I'm getting. Is it because I am not referencing rpt in my codebehind?
AlexW
If you're doing it straight from the code (and not using an ObjectDataSource), don't use DataSourceID, use DataSource.
Damien Dennehy
I've created a wrapper for my List of the DataSource type.I have asp:Repeater ID="myRepeater" and I have myRepeater.DataSource = OrderedTwitterData; myRepeater.DataBind(); but I get an error saying: "The DataSourceID of 'myRepeater' must be the ID of a control of type IDataSource. A control with ID 'twitterFeedSource' could not be found."
AlexW
Don't use DataSourceID *and* DataSource at the same time. Set one or the other. Remove the DataSourceID from your markup.
womp
Thanks @womp :D
AlexW
No problem, glad you got it sorted.
womp