Tuesday, January 22, 2013

Autonumber Rows in ASP.NET Repeater control

The easiest way to autonumber rows in Repeater is to use markup or ItemDataBound event. Since each row has ItemIndex property when created/bounded. Therefore for Repeater markup one can write the following:


< ItemTemplate>
    < %# Container.ItemIndex + 1 %>
< /ItemTemplate>

Or in the ItemDataBound event (for more complex rendering) this should look like so:

if ((e.Item.ItemType == ListItemType.AlternatingItem) || 
    (e.Item.ItemType == ListItemType.Item))
{
    int num = e.Item.ItemIndex + 1;

    // some code
}

No comments:

Post a Comment