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 に答える