Kevin
Kevin

Reputation: 4848

Use a dataset as a datasource for Repeater control?

I'm just learning ASP.Net, so I hope that you bear with me and my questions. In my program, I have a dataset that contains Url strings that point to various images. My question is, can I use that dataset as a datasource for a Repeater control so that the Repeater uses those Urls to display the images?

Thanks so much for any help and advice.

Upvotes: 1

Views: 10176

Answers (1)

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

You most certainly can. You will want to do the binding in the code-behind, probably in the Load method like this:

repeaterControl.DataSource = yourDataSet.Tables[0];
repeaterControl.DataBind();

where 0 is the index of the DataTable you're trying to get to.

Then you'll want to build the markup something like this:

<asp:Repeater ID="repeaterControl" runat="server">
    <ItemTemplate>
        <asp:Image runat="server"
            ImageUrl="<%# DataBinder.Eval(Container.DataItem, "TheFieldName") %>" />
    </ItemTemplate>
</asp:Repeater>

where TheFieldName is the name of the field/column in the DataTable that contains the URL. Now, this code may need to be debugged a little bit because I didn't build an entire project around this, but this will get you 99% of the way there, if not all of the way.

Upvotes: 6

Related Questions