2

I am a beginner to asp.net.So,can anyone please explain me the purpose and logic of using alternating item template in listview.

Ajay
  • 643
  • 2
  • 10
  • 27

2 Answers2

4

Alternating means every second item - so this can be used for applying different html or css effects to alternate rows, such as zebra striping of lists or tables.

So one of the respective templates will be applied to each row:

  • ItemTemplate - First Row
  • AlternatingItemTemplate - Second Row
  • ItemTemplate - 3
  • AlternatingItemTemplate - 4
  • ...

Without this, you would have to add code like

if (rowCount % 2 == 0)
{
   // Apply normal row formatting
}
else
{
   // Apply alternate row formatting
}

Doing this from the server, you'll have to fall back on a technique like using mod (%) any time you want more than 2 flavours of template.

Also note, that if the alternating style effect can be done only through css, that you can use the nth-child css 3 pseudo-class selector, and effects such as zebra striping which require only css style changes, e.g. by alternating classes, don't need to be done on the server side at all.

Community
  • 1
  • 1
StuartLC
  • 104,537
  • 17
  • 209
  • 285
0

Stuart answered the primary question. On the other hand, you might have to maintain two sets of the same html code. One for the item template, and one for the alternating item template. If you need to change sth on the row, you'll have to remember to duplicate the change for the alternating item template.

More here

and more here

Eric Burdo's answer is also interesting

Ricardo Appleton
  • 679
  • 10
  • 22