Wesley Skeen
Wesley Skeen

Reputation: 8285

Asp repeater accessing each line from an sql datasource

I have a repeater that on itemdatabound i want to access the row in the sqldatasource so i can get the id of an item.

I cant use hidden fields to do this so are there any other options? Thanks to all

Upvotes: 1

Views: 455

Answers (2)

James Johnson
James Johnson

Reputation: 46047

You can try something like this in the ItemDataBound event:

if (Repeater1.DataSource != null)
{
    int ID = ((DataTable)Repeater1.DataSource).Rows[e.Item.ItemIndex].Field<int>("ID");
}

Or this might work too:

int ID = (int)((DataRowView)e.Item.DataItem)["ID"];

Upvotes: 2

Adam Tuliper
Adam Tuliper

Reputation: 30152

In ItemDataBound try:

((DataRowView)e.Item.DataItem)["YourKey"] 

DataItem is your bound item, be it a custom class or a data row for example. This depends on how you are binding it but you didn't specify.

Upvotes: 1

Related Questions