2

I am trying to dynamically allocate a css class to a tr in an ItemTemplate in a asp:ListView. I want to apply this class if a boolean value in the backing model equals true. In this case the property is BackingModelProperty

In this answer the Visible property is being dynamically set based on the data the OP is trying to display in their asp:ListView.

So far I have tried:

<ItemTemplate>
    <tr runat="server" class="<%# (((bool)Eval("BackingModelProperty")) == true) ? 'test-css-class' : null %>">
    ...
</ItemTemplate>

However, I get an error

Type of conditional expression cannot be determined because there is no implicit conversion between char and <null>

So instead I tried using the CssClass attribute instead of class, this also didn't work. I have tried casting to a integer and checking whether the value was == 1. This also failed with the same error message.

Can anyone suggest where I am going wrong?

Michael Hancock
  • 2,673
  • 1
  • 18
  • 37

1 Answers1

4

Remove the runat=server (and do not use ')

<tr class="<%# (((bool)Eval("BackingModelProperty")) == true) ? "test-css-class" : null %>">

And make sure that BackingModelProperty is, or can be converted to a boolean.

VDWWD
  • 35,079
  • 22
  • 62
  • 79
  • This appears to have worked, thanks! Out of interest, why was the `runat=server` problematic? – Michael Hancock Feb 15 '18 at 10:11
  • 1
    Because then it becomes a `HtmlControl`, which would require binding it's properties different (this time with singe quotes: `Property='<%# Eval("Item") %>'`), and will require items of `HtmlTableCell` and presumably a parent `table runat=server`. By then you are wrapping a GridView inside another table AND server control. – VDWWD Feb 15 '18 at 10:25