If statement inside a ListView with Eval() or DataBinder.Eval()?
16,224
Solution 1
Untested, as I don't have Visual Studio available at the moment, but since HtmlTableRow has a Visible
property, the following should work:
<tr class="item" runat="server" Visible='<%# Convert.ToInt32(Eval("Type")) == 0 %>'>
...
</tr>
Solution 2
Here is the full code, made fancy and short.
<ItemTemplate>
<tr class="item">
<td colspan="<%# Eval(Container.DataItem,"Type")) == 0 ? 1:2%>">
<strong><%# Convert.ToDateTime(Eval("WorkDate")).ToShortDateString() %></strong>
</td>
<td style="text-align: center;" visible="<%# Eval(Container.DataItem,"Type")) == 1>">
<%# Eval("SkillName") %>
</td>
</tr>
</ItemTemplate>
Comments
-
ward87 almost 2 years
I have a listview control on an .aspx page. Inside this list view i want to check "Type" property which comes from database. here is the example code :
<ItemTemplate> <%# if(Convert.ToInt32(DataBinder.Eval(Container.DataItem,"Type")) == 0){ %> <tr class="item"> <td> <%# Convert.ToDateTime(Eval("WorkDate")).ToShortDateString() %> </td> <td style="text-align: center;"> <%# Eval("SkillName") %> </td> </tr> <%# } else if (Convert.ToInt32(DataBinder.Eval(Container.DataItem,"Type")) == 1) {%> <tr class="item"> <td colspan="2"> <strong><%# Convert.ToDateTime(Eval("WorkDate")).ToShortDateString() %></strong> </td> </tr> <% } %> </ItemTemplate>
As a last resort i tried to user DataBinder.Eval() but i get the exception "Expected class, delegate, enum, interface, or struct". What can i be doing wrong? Writing a function in code-behind isn't an option for me. Is there a way to achieve this?