views:

325

answers:

4

is it possible to do something like the following in ASP.NET:

<tr<%= index++ % 2 == 0 ? " class=\"alt-row\"" : ""; %>>

in other words, is there a way to escape the angle brackets for the inline code block or something?

(i know the alternative is:

<% if (index++ % 2 == 0) { %>
    <tr class="alt-row">
<% } else { %>
    <tr>
<% } %>

. i'm just curious if the other way is possible)

A: 

Have you tried it yet? A similar test worked just fine for me.

Andrew Hare
yes, i have and it hasn't worked for me. you just exactly what i did??
gabe
Could it just be the missing space between the tr and the <%? Can you post the resulting HTML?
Lazarus
A: 

I've used the <% %> construct inside tags to assign properties so I would imagine this would work. Did it not work?

Lazarus
+4  A: 

Yes, you can do this (at least, in MVC), though your example has a couple errors.

Here's a fixed version:

<tr<%= index++ % 2 == 0 ? " class=\"alt-row\"" : "" %>>
Craig Stuntz
ok, this worked. i was trying to avoid the space for the ones that don't have the class.
gabe
I updated the example to do that.
Craig Stuntz
The second error, BTW, was the extra semicolon at the end, which can't be used with %=
Craig Stuntz
the semi-colon fixed everything actually! what you have now is what i had except for w/ the semi-colon. i just put my original back in, removed the semi-colon, and it worked! thx!
gabe
A: 

Try this.

 <tr class="<%= index++ % 2 == 0 ? "alt-row" : "" %>">
Brandon