我正在尝试将 css 类动态分配给 tr
在 ItemTemplate
在 asp:ListView
。如果支持模型中的 bool 值等于 true,我想应用此类。在本例中,属性为 BackingModelProperty
在 this answer Visible
属性是根据 OP 尝试在其 asp:ListView
中显示的数据动态设置的。 .
到目前为止我已经尝试过:
<ItemTemplate>
<tr runat="server" class="<%# (((bool)Eval("BackingModelProperty")) == true) ? 'test-css-class' : null %>">
...
</ItemTemplate>
但是,我收到错误
Type of conditional expression cannot be determined because there is no implicit conversion between
char
and<null>
所以我尝试使用 CssClass
属性而不是 class
,这也行不通。我尝试将其转换为整数并检查该值是否为 == 1
。这也失败并出现相同的错误消息。
谁能告诉我哪里出错了?
最佳答案
删除runat=server
(并且不要使用'
)
<tr class="<%# (((bool)Eval("BackingModelProperty")) == true) ? "test-css-class" : null %>">
并确保 BackingModelProperty
是或可以转换为 bool 值。
关于c# - 动态添加类到 ASP.NET WebForm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48804356/