[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
In this tutorial viewers will learn how to style a decorative dropcap for the first letter in a paragraph using CSS. In CSS, create a new paragraph class by typing "p.fancy": Then, to style only the first letter of the first sentence, include the ps…
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…

656 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