2011-05-09 60 views
2

我跟着这个问题stuck on changing each row color during run-time in listview in asp.net based on database entries并试图在VB中做同样的事情,但我得到了一些无法解释的错误,例如对象引用未设置为对象的实例 最有可能此行=>
昏暗的细胞作为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

回答

5

对于这个工作,你必须确保你提供MainTableRow ID来tr元素,并将其标记为runat="server"即确保您的加价(HTML)是一样的东西

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

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

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

而在后台代码,有保护功能,提供基于数据(例如C#功能会)CSS类

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

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

+1

感谢维奈,这个工作 – indavinci 2011-08-21 06:48:40

2

并且根本没有任何代码。

我只是增加了一个领域称为状态 的SQL例如

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> 
相关问题