?
Solved

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

Posted on 2011-02-16
2
Medium Priority
?
1,045 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 23

Accepted Solution

by:
Snarf0001 earned 1000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this tutorial viewers will learn how to embed custom externally-hosted Google Fonts using the Google Font API in CSS Go to the Google Fonts website at google.com/fonts: Browse or search based on font properties or name to find a suitable font for…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

850 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