If inside a ListView with Eval () or DataBinder.Eval ()?

I have a listview control on an .aspx page. Inside this view list, I want to check the Type property, which comes from the database. here is a sample 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 use DataBinder.Eval (), but I get the exception "Expected class, delegation, enumeration, interface or structure". What can i do wrong? Writing a function in encoding is not an option for me. Is there any way to achieve this?

+4
source share
3 answers

Untested, since I don't have Visual Studio at the moment, but since HtmlTableRow has the Visible property, the following should work:

 <tr class="item" runat="server" Visible='<%# Convert.ToInt32(Eval("Type")) == 0 %>'> ... </tr> 
+2
source

Here is the complete 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> 
+4
source

yes, you will have to do some client side scripts though ... I would suggest jquery ..

you basically scroll through all the lines in jquery and based on the data in the line, you can change the innerhtml of the line object based on the ".item" selector to determine if it should be in one or the other format.

0
source

All Articles