2

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>

...

4

4 に答える 4

4

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.

于 2012-12-04T12:59:02.210 に答える
1
if(((YourDataTable) Container.DataItem).Enable){
}

だからあなたはすることができます:

<% if (((YourDataTable) Container.DataItem).Enable)
      { %>

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

     <% } %>
于 2012-12-04T12:58:44.603 に答える
1
ImageUrl=<%# (bool)Eval(condition)? "a.png" : "b.png" %>
于 2012-12-04T13:03:16.030 に答える
1

I think instead of doing this in .aspx file, you should handle ListView.ItemDataBound Event.

protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
    {

        if (e.Item.ItemType == ListViewItemType.DataItem)
        {

            Image imgActive = (Image)e.Item.FindControl("imgActive");
            System.Data.DataRowView rowView = e.Item.DataItem as System.Data.DataRowView;
            string value = rowView["Enable"].ToString();
            if (value == "1")
            {
                imgActive.ImageUrl="~/images/imageA.gif"; 
            }
            else
            {
                imgActive.ImageUrl="~/images/imageB.gif"; 
            }
        }
    }
于 2012-12-04T13:11:33.740 に答える