如何让列表视图行颜色根据行中的数据进行更改
我遵循这个问题坚持在基于数据库条目的asp.net listview中运行时更改每一行颜色并尝试在 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
非常感谢您的帮助。
dk
I followed this question stuck on changing each row color during run-time in listview in asp.net based on database entries and tried to do the same in VB but i am getting some unexplained errors, like Object reference not set to an instance of an object
most likely for this row =>
Dim cell As HtmlTableRow = DirectCast(e.Item.FindControl("MainTableRow"), mlTableRow)
Please let me know if there is any better way / correct way to do this in 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
Many thanks for your help.
dk
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为此,您必须确保向
tr
元素提供MainTableRow
id 并将其标记为runat="server"
即确保您的标记(html)类似于一种不同的(IMO,更简单)方法将使用数据绑定表达式。例如,在您的标记中,使用
And 在代码隐藏中,有一个受保护的函数来提供基于数据的 CSS 类(示例 C# 函数将是)
,最后,根据您的需要定义这些 css 类(row、altRow)。
For this to work, you must ensure that you provide
MainTableRow
id totr
element and mark it asrunat="server"
i.e. make sure that your mark-up (html) is something likeA different (and IMO, simpler) approach will be using data-binding expressions. For example, in your markup, use
And in code-behind, have a protected function to provide CSS class based on data (a example c# function will be)
And lastly, define those css classes (row, altRow) as per your needs.
并且根本没有任何代码隐藏。
我刚刚在 SQL 中添加了另一个名为 status 的字段
例如
然后在页面
中
and without any code behind at all.
I just added another field to the SQL called status
for example
then in the page
and
我知道这已经很旧了,但是如果有人在寻找没有 css 的内联(就像我一样),这是我的解决方案:
db 列“优先级”包含 0,1,2 等..并且我想将列表行着色为红色,蓝色,根据这些绿色:
和您定义的列表
I know this is old but if anyone looking for inline without css (like I was) here is my solution:
The db column 'Priority' contains 0,1,2, etc.. and i want to color my list rows red,blue,green according to those :
and your list defined