如何让列表视图行颜色根据行中的数据进行更改

发布于 2024-11-05 20:30:01 字数 931 浏览 1 评论 0原文

我遵循这个问题坚持在基于数据库条目的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼角的笑意。 2024-11-12 20:30:01

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

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

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

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

And 在代码隐藏中,有一个受保护的函数来提供基于数据的 CSS 类(示例 C# 函数将是)

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

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

For this to work, you must ensure that you provide MainTableRow id to tr element and mark it as runat="server" i.e. make sure that your mark-up (html) is something like

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

A different (and IMO, simpler) approach will be using data-binding expressions. For example, in your markup, use

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

And in code-behind, have a protected function to provide CSS class based on data (a example c# function will be)

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

And lastly, define those css classes (row, altRow) as per your needs.

人事已非 2024-11-12 20:30:01

并且根本没有任何代码隐藏。

我刚刚在 SQL 中添加了另一个名为 status 的字段
例如

select given, surname, case when owing > 1000 then 'Behind' else 'OK' end as Status from cust

然后在页面

                <ItemTemplate>
                <tr class='<%# Eval("Status") %>' style="">

    <style type="text/css">

    .behind
    {    
         font-style :italic ;
         color: black  ;
    }
    .ok
    {    
         color: grey  ;
    }

</style>

and without any code behind at all.

I just added another field to the SQL called status
for example

select given, surname, case when owing > 1000 then 'Behind' else 'OK' end as Status from cust

then in the page

                <ItemTemplate>
                <tr class='<%# Eval("Status") %>' style="">

and

    <style type="text/css">

    .behind
    {    
         font-style :italic ;
         color: black  ;
    }
    .ok
    {    
         color: grey  ;
    }

</style>
无风消散 2024-11-12 20:30:01

我知道这已经很旧了,但是如果有人在寻找没有 css 的内联(就像我一样),这是我的解决方案:

db 列“优先级”包含 0,1,2 等..并且我想将列表行着色为红色,蓝色,根据这些绿色:

<ItemTemplate>
            <div style='<%# color:" + mylistof_PRIORITYCOLORS[Convert.ToInt16(Eval("Priority"))] %>'>

和您定义的列表

public static List<string> mylistof_PRIORITYCOLORS = new List<string> { "Red", "Blue", "Green" };

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 :

<ItemTemplate>
            <div style='<%# color:" + mylistof_PRIORITYCOLORS[Convert.ToInt16(Eval("Priority"))] %>'>

and your list defined

public static List<string> mylistof_PRIORITYCOLORS = new List<string> { "Red", "Blue", "Green" };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文