View Single Post
  #1 (permalink)  
Old August 9th, 2012, 05:40 PM
RichardL1969 RichardL1969 is offline
Registered User
Points: 27, Level: 1
Points: 27, Level: 1 Points: 27, Level: 1 Points: 27, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2012
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
Default How to dynamically set a listview's backColor in code behind

Hello

Is there a way of dynamically changing the background colour for a whole row of a listview in the .cs file?

I have asp.net code which defines an item template as...

Code:
        <ItemTemplate >
        <%# addDateToListViewIfChanged() %>
            <tr  class="FixtureStyle" style="">
              <asp:Panel ID="RowPanel" runat="server">
            
                <td style="text-align:right">
                    <asp:Label ID="HomeTeam" runat="server" Text='<%# Eval("ClubName") %>' Width="160px"   />
                </td>
                <td style="width:20px; text-align:center">
                    <asp:Label ID="Versus" runat="server" Text=" v " />
                </td>
                <td>
                    <asp:Label ID="AwayTeam" runat="server" Text='<%# Eval("Expr1") %>' Width="160px"/>
                </td>
                <td style="text-align:right">
                    <asp:Label ID="HomeScoreLabel" runat="server" Text='<%# Eval("HomeScore") %>' Width="30px" />
                </td>
                <td style="width:18px; text-align:center">
                    <asp:Label ID="Dash" runat="server" Text = " - " />
                </td>
                <td>
                    <asp:Label ID="AwayScoreLabel" runat="server" Text='<%# Eval("AwayScore") %>' Width="30px"/>
                </td>
              </asp:Panel>
            </tr>
        </ItemTemplate>
...and I have code behind code which finds the data I want and highlights the labels correctly.

Code:
    protected void ResultsListView_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            // 1. Find the specific Label and check text value.
            Label hlbl = (Label)e.Item.FindControl("HomeTeam");
            Label albl = (Label)e.Item.FindControl("AwayTeam");
            Label hslbl = (Label)e.Item.FindControl("HomeScoreLabel");
            Label aslbl = (Label)e.Item.FindControl("AwayScoreLabel");
            Label vlbl = (Label)e.Item.FindControl("Versus");
            Label dlbl = (Label)e.Item.FindControl("Dash");
            Panel RPanel = (Panel)e.Item.FindControl("RowPanel");
            

            if (hlbl.Text == "Middle Ripsley" | albl.Text == "Middle Ripsley")
            {
                hlbl.BackColor = System.Drawing.ColorTranslator.FromHtml("#333333");
                albl.BackColor = System.Drawing.ColorTranslator.FromHtml("#333333");
                hslbl.BackColor = System.Drawing.ColorTranslator.FromHtml("#333333");
                aslbl.BackColor = System.Drawing.ColorTranslator.FromHtml("#333333");
                vlbl.BackColor = System.Drawing.ColorTranslator.FromHtml("#333333");
                dlbl.BackColor = System.Drawing.ColorTranslator.FromHtml("#333333");
                RPanel.BackColor = System.Drawing.ColorTranslator.FromHtml("#333333");
            }
        }
    }

Unfortunately, only the label parts of the row are highlighted, with the rest staying the original black, as shown below.


http://img.photobucket.com/albums/v6...chighlight.png


(note the pabel was added afterwards in the hope that I could set the background for the panel, but that didn't work either)


Is there a way of setting the whole row so it is highlighted?
Reply With Quote