Search code examples
c#asp.netlistviewconditional-statementsitemtemplate

ASP.NET : Conditional Logic for 2 diff values in <TD> in ItemTemplate


Goal: I have two different columns in a database. Depending on which client it is, I will either want to display the value from one column, or the other in this single (which is inside an ItemTemplate associated to a LayoutTemplate).

Research: I tried looking up similar articles on SO, but they were for slightly different problems, including this one: ASP.Net: Conditional Logic in a ListView's ItemTemplate

Error: I tried using an if statement in the aspx but it tells me I am missing ; even though I have it:

default.aspx:

 <td><%# if(isAAA()) { Item.} else { Item.}; %></td>

I also tried:

  <td><%# if(isAAA()) { Item.; } else { Item.; } %></td>

I also tried:

   <td><% if(isAAA()) { Item.; } else { Item.; } %></td>

I also tried:

       <td><% if(isAAA()) { #Item.; } else { #Item.; } %></td>

I also tried the following, but it placed the value under the wrong column, and it displayed part of the aspx logic on the page:

if(isAAA() { <td><%# Item.%></td> else { <td><%# Item.%></td> }

Note: isAAA() is a method in the .cs file that checks a value in the Settings file.


Solution

  • I solved it with the following setup:

    <% if (is()) %> 
                <% {  %>   
                     <td><%# Item. %></td> 
                <% } %>
    
                <% else %>
                <% { %>
                      <td><%# Item. %></td>
                <% } %>
    

    This was the closest SO I found : ASP.NET Conditional Markup Rendering According to Web.config Key