Home > Blockchain >  BackColor in codebehind only changing color directly around text
BackColor in codebehind only changing color directly around text

Time:12-13

So I have a list view with a C# code behind. I'm attempting to set the background of the cell if the value of JobsCovered is > 1000 (for example) in my code behind. But instead of setting the background color of the entire cell, it only sets the background immediately around the text only. I have to be missing something very simple.

HTML:

<ItemTemplate>
        <td runat="server" style="background-color:black;color: white;text-align:center;">
        <asp:Label ID="PartDescriptionLabel" runat="server" Text='<%# Eval("PartDescription") %>' />
        <br />
        <asp:Label ID="PartNumberLabel" runat="server" Text='<%# Eval("PartNumber") %>' />              
        <br />
        <div id="JobsCoveredBox" style="height:60px; line-height:60px">
        <asp:Label ID="JobsCoveredLabel" runat="server" Text='<%# Eval("JobsCovered") %>' />
        </div>
        </td>
</ItemTemplate>

Code Behind:

protected void RepackOSR_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            // Retrieve the current item.
            ListViewItem item = e.Item;

            Label JobsCoveredLabel;
            // Verify if the item is a data item.
            if (item.ItemType == ListViewItemType.DataItem)
            {
                JobsCoveredLabel = (Label)e.Item.FindControl("JobsCoveredLabel");
                System.Data.DataRowView rowView = e.Item.DataItem as System.Data.DataRowView;
                //Store the value of "JobsCovered" from data source
                Int32 currentJobCovered = Convert.ToInt32(rowView["JobsCovered"].ToString());
                if (currentJobCovered > 1000)
                {
                    //Why won't this change the entire background of the cell?
                    JobsCoveredLabel.BackColor = Color.Green;
                }
            }
        }

It only does this...

Not the entire background

CodePudding user response:

You want of course to color the div background I assume?

thus you need to tag the div as server control.

So, try this:

 <div id="JobsCoveredBox" runat="server" style="height:60px; line-height:60px">

So, now we can grab/get the div say like this in your row data bind event

        HtmlGenericControl mydiv = (HtmlGenericControl)e.Item.FindControl("JobsCoveredBox");

        mydiv.Style.Add("background-color", "aliceblue");
  • Related