Prathamesh Gharat
Prathamesh Gharat

Reputation: 514

Ternary operation in asp.net in vb.net

Am trying to add an 'odd' class to a table row in a repeater control..

    <tr class="<%# If((Container.ItemIndex % 2 == 0), "even", "odd") %>">

I have verified that Container.ItemIndex returns the index number.

This causes it to throw this error.. First operand in a binary 'If' expression must be nullable or a reference type.

I also tried replacing the % with Mod keyword but it throws an "Expression expected" error..

Am new to coding asp.net in vb, tried searching for a solution but failed to get a working solution..

Upvotes: 2

Views: 1369

Answers (5)

Jignesh Rajput
Jignesh Rajput

Reputation: 3558

you can also try this :

 <tr class='<%# ((Container.ItemIndex % 2 == 0) ? "odd": "even") %>' >

Hops its helps

Upvotes: 1

mgnoonan
mgnoonan

Reputation: 7200

If your target audience have browsers that implement the right CSS3 features, you can also accomplish this task purely with CSS:

http://www.w3.org/Style/Examples/007/evenodd.en.html

Upvotes: 0

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112772

Try

<tr class="<%# If((Container.ItemIndex Mod 2 = 0), "even", "odd") %>"> 

Upvotes: 1

andygjp
andygjp

Reputation: 2464

To add to Amritpal answer, you should use Mod not %.

Upvotes: 1

Amritpal Singh
Amritpal Singh

Reputation: 1785

you should use only = instead of == and use mod

Upvotes: 6

Related Questions