gpt4 book ai didi

asp.net - 如何根据行中的数据更改 ListView 行颜色

转载 作者:行者123 更新时间:2023-12-02 07:47:23 24 4
gpt4 key购买 nike

我关注了这个问题 stuck on changing each row color during run-time in listview in asp.net based on database entries并尝试在 VB 中做同样的事情,但我遇到了一些无法解释的错误,比如未将对象引用设置为对象的实例 这行最有可能 =>
Dim cell As HtmlTableRow = DirectCast(e.Item.FindControl("MainTableRow"), mlTableRow)

请告诉我是否有更好的方法/正确的方法在 VB 中执行此操作?

Protected Sub ListView2_ItemDataBound1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) _
Handles ListView2.ItemDataBound
If e.Item.ItemType = ListViewItemType.DataItem Then
Dim dataitem As ListViewDataItem = DirectCast(e.Item, ListViewDataItem)
Dim mstorename As String = DataBinder.Eval(dataitem.DataItem, "Store")
If mstorename = "A1" Then
Dim cell As HtmlTableRow = DirectCast(e.Item.FindControl("MainTableRow"), mlTableRow)
cell.BgColor = #E0E0E0
End If
End If
End Sub

非常感谢您的帮助。

你好

最佳答案

为此,您必须确保向 tr 元素提供 MainTableRow id 并将其标记为 runat="server" 即确保您的标记 (html) 类似于

<ItemTemplate>
<tr id="MainTableRow" runat="server">
...

一种不同的(和 IMO,更简单的)方法将使用数据绑定(bind)表达式。例如,在您的标记中,使用

<ItemTemplate>
<tr class='<%# GetRowStyle(Container.DataItem) #>'>

并且在代码隐藏中,有一个 protected 函数来提供基于数据的 CSS 类(一个示例 c# 函数将是)

protected string GetRowStyle(object item)
{
var store = DataBinder.Eval(item, "Store");
if (store == "A1")
{
return "altRow";
}
else
{
return "row";
}
}

最后,根据您的需要定义那些 css 类(行、altRow)。

关于asp.net - 如何根据行中的数据更改 ListView 行颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5933114/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com