Search code examples
c#asp.netimagelistviewitemtemplate

How I can make a If Clause in my ASP.NET ListView?


I have a ASP.NET Application with a ListView. The ListView get Datafrom a DataTable and I want if my ListView builting the itemtemplate that I have a if clause for change a image inside.

ForExample:

My DataTable have a Column with name Enable. This can have two Values. The first Value is 0 and the second 1. If in my Columns is the Value 0 I want the a.png and if I have 1 a other image in my asp:image control (in my List View).

Here is my aspx site:

...

   <ItemTemplate>
        <tr onmouseover="this.style.backgroundColor='#87CEFA'"
        onmouseout="this.style.backgroundColor='#ffffff'">
            <td align="left"><span class="spanpading"><asp:Label ID="lblname" Text='<%# Eval("NAME") %>' runat="server"  /></span></td>
            <td align="left"><span class="spanpading"><asp:Label ID="lblcompany" Text='<%# Eval("COMPANY") %>' runat="server"  /></span></td>
            <td align="left"><span class="spanpading"><asp:Label ID="lblVon" Text='<%# Eval("TIMEFROM") %>' runat="server"  /></span></td>
            <td align="left"><span class="spanpading"><asp:Label ID="lblBis" Text='<%# Eval("TIMETO") %>' runat="server"  /></span></td>
            <td align="left"><span class="spanpading"><asp:Label ID="lblErsteller" Text='<%# Eval("CREATOR") %>' runat="server"  /></span></td>
            <td align="left"><asp:ImageButton ID="imgDelete" runat="server" ToolTip="löschen" ImageUrl="images/delete.gif" CommandName="DeleteClick" CommandArgument='<%# Container.DataItemIndex %>' /></td>
            <td align="left"><asp:ImageButton ID="imgUpdate" runat="server" ToolTip="ändern" ImageUrl="images/edit.gif" CommandName="UpdateClick" CommandArgument='<%# Container.DataItemIndex %>' /></td>
            <td align="left"><asp:ImageButton ID="imgEmail" runat="server" ToolTip="Zugangsdaten senden" ImageUrl="images/send.gif" CommandName="SendClick" CommandArgument='<%# Container.DataItemIndex %>' /></td>
           <% if ()
              { %>

             <td align="left"><asp:Image ID="imgActive" runat="server" ToolTip="Aktiv" Width="25px" Height="25px" ImageUrl="images/yes.gif"/></td>

             <% } %>

            <td><asp:Label ID="lblID" runat="server" Text='<%# Eval("ID") %>' Visible="False" ></asp:Label></td>
        </tr>
    </ItemTemplate>

...


Solution

  • You could use a block element like e.g. <span>, make it run at server and toggle its visibility, based on a data binding element.

    E.g.:

    <span runat="server" Visible='<%# Eval("IsConditionTrue") %>'>
        <!-- ... Place your conditionally visible tags here inside ... -->
    </span>
    

    If you cannot evaluate to a single condition, you could also use a more complex statement like e.g.

    <span runat="server" 
          Visible='<%# (int)Eval("SomeValue")==1 && (bool)Eval("SomeOtherValue") %>'>
        <!-- ... Place your conditionally visible tags here inside ... -->
    </span>
    

    Finally to get to your concrete example, I think you could do something like:

    <span runat="server" Visible='<%# (int)Eval("Enable")==1 %>'>
        <asp:Image ImageUrl="images/yes.gif" />
    </span>
    <span runat="server" Visible='<%# (int)Eval("Enable")!=1 %>'>
        <asp:Image ImageUrl="images/no.gif" />
    </span>
    

    Of course, Mhd. Yasseen's answer seems to be the shortest for your case:

    <asp:Image ImageUrl='<%# (int)Eval("Enable")==1 ? "yes.gif" : "no.gif" %>' />
    

    Please note that you have to add additional attributes like runat="server" to your Image tag, just as in your original question. I've omitted them for readability in my code above.