Michael Hancock
Michael Hancock

Reputation: 2855

Dynamically adding class to a ASP.NET WebForm

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?

Upvotes: 2

Views: 1928

Answers (1)

VDWWD
VDWWD

Reputation: 35524

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.

Upvotes: 5

Related Questions