ASP.NET tip: Autonumber Rows in ASP.NET Repeater control
Q: How to print the current row number in an ASP.NET Repeater control ?
A: 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 }
Enjoy :)
Saturday, April 26, 2008 2:37 AM