Solved

Mysterioso ASP: <tr class="shade<%# Container.ItemIndex Mod 2 %>">

Posted on 2011-02-16
2
989 Views
Last Modified: 2012-05-11
The ASP.NET shown in expression 1 produces the HTML shown in expression 2:

1) <tr class="shade<%# Container.ItemIndex Mod 2 %>">
2)       <tr class="shade0">...</tr>
          <tr class="shade1">...</tr>
          <tr class="shade0">...</tr>
          <tr class="shade1">...</tr>
          ...

I have to admit, the "Container.ItemIndex Mod 2" has me mystified.  

Is Container a namespace and ItemIndex a method?

I found numerous pages explaining how to use "Container.ItemIndex Mod 2", but none that explained what was going on.
0
Comment
Question by:jdana
2 Comments
 
LVL 22

Accepted Solution

by:
Snarf0001 earned 250 total points
ID: 34913026
"Container" is actually just a keyword that's used only in inline databinding expressions <%# %> to get a reference to the parent or databound item.

If used within a repeater control, "Container" will return a RepeaterItem instance.
If used within a DataList, it will return a DataListItem instance.

So really you need to look at the corresponding class there.  Both DataListItem and RepeaterItem contain an .ItemIndex property, which is simply that, the index of the current item among the databound source.

Mod is a mathematical operater to return the modulus or remainder of a division.

So in a nutshell, if you were binding the following to a Repeater control:
"Frank"
"John"
"Steve"

As the control performs its binding, each of these will in turn be assigned to the "Container" keyword.
Frank -> ItemIndex = 0
John -> ItemIndex = 1
Steve -> ItemIndex = 2

So, for frank, the remainder of itemIndex (0) / 2 = 0.  For John, the remainder of ItemIndex (1) / 2 = 1.  For Steve, ItemIndex (2) / 2 = 0.

So it's just an easy way to assign alternating differences (backcolors etc) as the control is bound.  The .ItemIndex Mod 2 will always return 0,1,0,1,0,1....
0
 

Author Closing Comment

by:jdana
ID: 34948238
Snarf0001,

Your explanation is one of the best explanations I've read over the last 3 years of using Experts Exchange.  It's well written, and, better yet, provides some terrific examples.  

Thanks.

J
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
In this tutorial viewers will learn how to style rounded corners for elements in CSS using the border-radius property Begin with a normal styled element such as a div: To style all four corners of the div to be the same degree of roundness, use the …
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now