gabe
gabe

Reputation: 1950

Is there a way to nest (or escape) ASP.NET inline code inside HTML angle brackets?

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)

Upvotes: 0

Views: 918

Answers (4)

Craig Stuntz
Craig Stuntz

Reputation: 126577

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\"" : "" %>>

Upvotes: 5

Brandon
Brandon

Reputation: 70002

Try this.

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

Upvotes: 0

Lazarus
Lazarus

Reputation: 43094

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

Upvotes: 0

Andrew Hare
Andrew Hare

Reputation: 351566

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

Upvotes: 0

Related Questions