p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 4 General Discussion (http://p2p.wrox.com/forumdisplay.php?f=561)
-   -   How to dynamically set a listview's backColor in code behind (http://p2p.wrox.com/showthread.php?t=88205)

RichardL1969 August 9th, 2012 05:40 PM

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?

All times are GMT -4. The time now is 07:00 AM.

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