Search code examples
asp.nethtmlasp.net-mvcsyntaxinline-code

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)


Solution

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