简体   繁体   English

动态将类添加到ASP.NET WebForm

[英]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 . 我正在尝试为ItemTemplate asp:ListViewItemTemplate中的tr动态分配一个CSS类。 I want to apply this class if a boolean value in the backing model equals true. 如果支持模型中的布尔值等于true,我想应用此类。 In this case the property is BackingModelProperty 在这种情况下,属性为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 . 此答案中 ,根据OP试图在其asp:ListView显示的数据来动态设置Visible属性。

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> 无法确定条件表达式的类型,因为char<null>之间没有隐式转换

So instead I tried using the CssClass attribute instead of class , this also didn't work. 因此,相反,我尝试使用CssClass属性而不是class ,这也不起作用。 I have tried casting to a integer and checking whether the value was == 1 . 我尝试将其转换为整数并检查该值是否== 1 This also failed with the same error message. 这也失败,并显示相同的错误消息。

Can anyone suggest where I am going wrong? 谁能建议我要去哪里错了?

Remove the runat=server (and do not use ' ) 删除runat=server (不要使用'

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

And make sure that BackingModelProperty is, or can be converted to a boolean. 并确保BackingModelProperty是或可以转换为布尔值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM