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? Than开发者_JS百科ks to all
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"];
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.
精彩评论