Solved

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

Posted on 2011-02-16
2
1,002 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 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
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 Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

733 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