Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old August 9th, 2012, 05:40 PM
Registered User
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


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...

        <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 style="width:20px; text-align:center">
                    <asp:Label ID="Versus" runat="server" Text=" v " />
                    <asp:Label ID="AwayTeam" runat="server" Text='<%# Eval("Expr1") %>' Width="160px"/>
                <td style="text-align:right">
                    <asp:Label ID="HomeScoreLabel" runat="server" Text='<%# Eval("HomeScore") %>' Width="30px" />
                <td style="width:18px; text-align:center">
                    <asp:Label ID="Dash" runat="server" Text = " - " />
                    <asp:Label ID="AwayScoreLabel" runat="server" Text='<%# Eval("AwayScore") %>' Width="30px"/>
...and I have code behind code which finds the data I want and highlights the labels correctly.

    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.


(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?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamically determining the name of an attribute-set Shadowgopher XSLT 1 July 17th, 2009 04:23 PM
how to set the backgound-color dynamically Neha XSLT 7 July 10th, 2008 07:17 AM
Dynamically set Button atrubute dijomonmd Javascript 1 June 8th, 2006 02:27 AM
Crystal - Set Y axis values dynamically vasanti Classic ASP Basics 0 February 14th, 2005 06:25 AM
Dynamically set selected attribute fs22 Javascript How-To 5 December 24th, 2004 04:10 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.