动态添加类到 ASP.NET WebForm

Mic*_*ock 2 c# asp.net webforms

我正在尝试将 css 类动态分配给 a trin an ItemTemplatein a asp:ListView。如果支持模型中的布尔值等于 true,我想应用此类。在这种情况下,该属性是BackingModelProperty

此答案中,该Visible属性是根据 OP 尝试在其asp:ListView.

到目前为止我已经尝试过:

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

但是,我收到错误

char无法确定条件表达式的类型,因为和之间没有隐式转换<null>

因此,我尝试使用该CssClass属性而不是class,这也不起作用。我尝试将其转换为整数并检查该值是否为== 1. 这也失败并出现相同的错误消息。

谁能建议我哪里出错了?

VDW*_*WWD 5

删除runat=server(并且不要使用'

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

并确保它BackingModelProperty是或可以转换为布尔值。