I often subscribe to the ItemDataBound event in a repeater to perform custom logic for each item within the repeater (for example, performing some custom calculations). When the repeater includes a header though, more often than not, you want to skip over the header row, since you don’t want to perform your custom calculations on that row. Until now, the way I did this was like this:
private void OnItemDataBound(object sender, RepeaterItemEventArgs e) {
// skip header row
if (e.Item.ItemIndex < 0) {
return;
}
// custom logic
}
Today however, I found out there’s a nicer way:
private void OnItemDataBound(object sender, RepeaterItemEventArgs e) {
// skip header row
if (e.Item.ItemType == ListItemType.Header) {
return;
}
// custom logic
}
Sure, given that both ways work equally well, it seems as though the difference is for aesthetics only (i.e it’s nicer to read); but there are actually two other big advantages:
- It is not tied into the implementation of the repeater - that is, it does not rely on the header row index being less than zero; and
- You can use the ListItemType enumeration to detect many different types of items - for example, the Footer row for performing summary calculations.