1

I am trying to change the color of the background when twa is at different values but it with my current code, the data is not firing, but when I remove the

   <asp: Label id="TWAlabel" OnDataBinding="ListView2_OnDataBinding">

But when I remove OnDataBinding the data fires, so I have no idea what is wrong. I am doing this in Asp.net(C#), JavaScript, and the data is sourced from an Access database. Here is my code:

 <style>
    .YellowThis
    {
    background-color:yellow;

 } 

 .RedThis
  {
 background-color:red;
 }

    .GreenThis  
    {
background-color:green;
 }

</style>        <asp:ListView id="ListView2" runat="server" DataSourceID="SqlDataSource3"  EnableViewState="False">

    </InsertItemTemplate>
    <ItemTemplate>
    <span style="background-color: white;color: #333333; border: 2em; border-width:1em; border-color:black;">
    Plant Name:
    <asp:Label id="PlantLabel" runat="server" Text='<%# Eval("Plant") %>' />
    <br />
    Department #:
    <asp:Label id="column1Label" runat="server" Text='<%# Eval("column1") %>' />
    <br />
    Department Name:
    <asp:Label id="GroupLabel" runat="server" Text='<%# Eval("Group") %>' />
    <br />
    Job Code:
    <asp:Label id="Job_CodeLabel" runat="server" Text='<%# Eval("Job_Code") %>' />
    <br />
    TWA:
    <asp:Label id="TWALabel" OnDataBinding="ListView2_DataBinding" runat="server" Text='<%# Eval("TWA") %>' />
    <br />
    Job Classification:
    <asp:Label id="Job_ClassificationLabel" runat="server" Text='<%# Eval("Job_Classification") %>' />
    <br />
    Job Function:
    <asp:Label id="Job_FunctionLabel" runat="server" Text='<%# Eval("Job_Function") %>' />
    <br />
    Job Description:
    <asp:Label id="Job_DescriptionLabel" runat="server" Text='<%# Eval("Job_Description") %>' />
    <br />
    <br />
    </span>
    </ItemTemplate>
    <LayoutTemplate>
    <div id="itemPlaceholderContainer" runat="server"  style="font-family: Verdana, Arial, Helvetica, sans-serif; text-align:center; border:3em; border-color:black;">
    <span runat="server" id="itemPlaceholder" />
    </div>
    <div style="text-align: left;background-color: white;font-family: Verdana, Arial, Helvetica, sans-serif;text-decoration:underline ;color: #FFFFFF;" >
    </div>
    </LayoutTemplate>
    <SelectedItemTemplate>
    <span style="background-color: white;font-weight: bold;color: #333333;">
    <u>Plant Name</u>:
    <asp:Label id="PlantLabel" runat="server" Text='<%# Eval("Plant") %>' />
    <br />
    Department #:
    <asp:Label id="column1Label" runat="server" Text='<%# Eval("column1") %>' />
    <br />
    Department Name:
    <asp:Label id="GroupLabel" runat="server" Text='<%# Eval("Group") %>' />
    <br />
    Job Code:
    <asp:Label id="Job_CodeLabel" runat="server" Text='<%# Eval("Job_Code") %>' />
    <br />
    TWA:
    <asp:Label id="TWALabel" runat="server" Text='<%# Eval("TWA") %>' />
    <br />
    Job Classification:
    <asp:Label id="Job_ClassificationLabel" runat="server" Text='<%# Eval("Job_Classification") %>' />
    <br />
    Job Function:
    <asp:Label id="Job_FunctionLabel" runat="server" Text='<%# Eval("Job_Function") %>' />
    <br />
    Job Description:
    <asp:Label id="Job_DescriptionLabel" runat="server" Text='<%# Eval("Job_Description") %>' />
    <br />
    <br />
    </span>
    </SelectedItemTemplate>
    </asp:ListView>


    <script runat="server">
    protected void ListView2_DataBinding(object sender, EventArgs e)
  {
      Label lbl = (Label)(sender);
 int TWA = (int)(Eval("TWA"));
lbl.Text = TWA.ToString();
if (TWA >= 85)
{
    if (TWA < 90)
    {
        lbl.CssClass = "YellowThis";
    }
    else
    {
        lbl.CssClass = "RedThis";
    }
}
else 
{
lbl.CssClass="GreenThis";
}
 }
</script>
4

2 回答 2

0

你可以在后面的代码中做到这一点。遍历您的列表项。

protected void add_color()
{

foreach(ListItem li in ListView2.Items)
{ 
       Label your_label = (Label)li.FindControl" your_label_name";      
       int TWA = Convert.ToInt32(your_label);



   if (TWA > 90)
                {

                       your_label.BackColor = System.Drawing.Color.Yellow 

                }

}
}
于 2013-07-12T21:57:44.320 回答
0

这个问题已经超过 1 年了,我不确定它是否会帮助这里的任何人。

我认为您的代码中出现的问题是因为您的 listview 数据绑定事件是在 Label 中创建的,而不是在 Listview 中创建的。

您可以做的是在 ListView 控件中创建一个数据绑定事件并读取 TWA 标签的值,然后设置您的 ListView 控件背景颜色。

于 2015-06-30T03:20:10.863 回答