?
Solved

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

Posted on 2011-02-16
2
Medium Priority
?
1,018 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
In this tutorial viewers will learn how to style different bullet points for unordered lists in CSS. Begin with a normal unordered list; the default bullet point is a solid circle: In the CSS, create a defined class of unordered list by typing "ul.S…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
Suggested Courses

762 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