-2

我有这个 c# 脚本,它根据 TWA 的值更改文本的颜色。虽然它主要工作,但它不会改变,如果

TWA value is >=85 (where it should be yellow) and >= 90 (where it should be red).

如何解决这个问题?

这是脚本:

protected System.Drawing.Color GetColorForLabel(string text)
{
    int theTWAValue;
    if (text != null && int.TryParse(text, out theTWAValue) && theTWAValue >= 0)
    {
        return (theTWAValue < 90) ? System.Drawing.Color.Yellow : System.Drawing.Color.Red;
    }

    return System.Drawing.Color.Green;
}

附加信息:

直到用户从下拉列表中选择某个工作代码并且所有数据值都来自一个access database

Edit2:即使debugging我还没有找到解决方案,对我的 if 语句失败的原因提供一些帮助也会很棒

编辑3:这是其余的代码:

<asp:SqlDataSource id="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:2007 SoundAssist VER 1.0.5  05-12-2011 ( 2013-06-24)ConnectionString %>" ProviderName="<%$ ConnectionStrings:2007  SoundAssist VER 1.0.5  05-12-2011 ( 2013-06-24)ConnectionString.ProviderName %>" SelectCommand="SELECT [Plant] FROM [PLANT]">
  </asp:SqlDataSource>
  <asp:DropDownList id="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="Plant" DataValueField="Plant" Height="85px" Width="393px">
  </asp:DropDownList>
  <asp:SqlDataSource id="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:2007  SoundAssist VER 1.0.5  05-12-2011 ( 2013-06-24)ConnectionString %>" ProviderName="<%$ ConnectionStrings:2007  SoundAssist VER 1.0.5  05-12-2011 ( 2013-06-24)ConnectionString.ProviderName %>" SelectCommand="SELECT [Plant], [Group No#] AS column1, [Group] FROM [Temp Table that contains TWA values] WHERE ([Plant] = ?)">
      <SelectParameters>
          <asp:ControlParameter ControlID="DropDownList1" Name="Plant" PropertyName="SelectedValue" Type="String" />
      </SelectParameters>
  </asp:SqlDataSource>
  <asp:DropDownList id="DropDownList2" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource2" DataTextField="Group" DataValueField="column1" Height="30px" Width="394px">
  </asp:DropDownList>
  <asp:SqlDataSource id="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:2007  SoundAssist VER 1.0.5  05-12-2011 ( 2013-06-24)ConnectionString %>" ProviderName="<%$ ConnectionStrings:2007  SoundAssist VER 1.0.5  05-12-2011 (2013-06-24)ConnectionString.ProviderName %>" SelectCommand="SELECT [Plant], [Group No#] AS column1, [Group], [Job Code] AS Job_Code, [Job Function] AS Job_Function, [Job Classification] AS Job_Classification FROM [Temp Table that contains TWA values] WHERE (([Plant] = ?) AND ([Group No#] = ?))">
      <SelectParameters>
          <asp:ControlParameter ControlID="DropDownList1" Name="Plant" PropertyName="SelectedValue" Type="String" />
          <asp:ControlParameter ControlID="DropDownList2" Name="column1" PropertyName="SelectedValue" Type="String" />
      </SelectParameters>
  </asp:SqlDataSource>
  <asp:DropDownList id="DropDownList3" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource3" DataTextField="Job_Classification" DataValueField="Job_Classification" Height="17px" Width="384px">
  </asp:DropDownList>
  <asp:ListView id="YourListView" OnLoad="YourListView_Load" runat="server"  DataSourceID="SqlDataSource4">
      <ItemTemplate>
          <span style="">Plant:
          <asp:Label id="PlantLabel" runat="server" Text='<%# Eval("Plant") %>' />
          <br />
          column1:
          <asp:Label id="column1Label" runat="server" Text='<%# Eval("column1") %>' />
          <br />
          Group:
          <asp:Label id="GroupLabel" runat="server" Text='<%# Eval("Group") %>' />
          <br />
          Job_Code:
          <asp:Label id="Job_CodeLabel" runat="server" Text='<%# Eval("Job_Code") %>' />
          <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 />
          TWA:
          <asp:Label id="TWALabel" runat="server" Text='<%# Eval("TWA") %>'  ForeColor='<%# GetColorForLabel( Eval("TWA") as string ) %>'/>
          <br />
          <br />
          </span>
      </ItemTemplate>

      </asp:ListView>
  <asp:SqlDataSource id="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:2007  SoundAssist VER 1.0.5  05-12-2011 ( 2013-06-24)ConnectionString %>" ProviderName="<%$ ConnectionStrings:2007  SoundAssist VER 1.0.5  05-12-2011 ( 2013-06-24)ConnectionString.ProviderName %>" SelectCommand="SELECT [Plant], [Group No#] AS column1, [Group], [Job Code] AS Job_Code, [Job Classification] AS Job_Classification, [Job Function] AS Job_Function, [Job Description] AS Job_Description, [TWA] FROM [Temp Table that contains TWA values] WHERE (([Plant] = ?) AND ([Group No#] = ?) AND ([Job Classification] = ?))">
      <SelectParameters>
          <asp:ControlParameter ControlID="DropDownList1" Name="Plant" PropertyName="SelectedValue" Type="String" />
          <asp:ControlParameter ControlID="DropDownList2" Name="column1" PropertyName="SelectedValue" Type="String" />
          <asp:ControlParameter ControlID="DropDownList3" Name="Job_Classification" PropertyName="SelectedValue" Type="String" />
      </SelectParameters>
  </asp:SqlDataSource>

我仍然无法修复错误,因此输入仍然非常有价值

4

4 回答 4

1

textnull或int 。由于无法转换的输入,TryParse失败。

int theTWAValue;
string text = "95";
if (text != null && int.TryParse(text, out theTWAValue) && theTWAValue >= 0)
{
    Console.WriteLine((theTWAValue < 90) ? "System.Drawing.Color.Yellow" : "System.Drawing.Color.Red");
}

打印:System.Drawing.Color.Red

于 2013-07-17T19:19:43.380 回答
0

Change the decision:

return (theTWAValue < 90) ? System.Drawing.Color.Yellow : System.Drawing.Color.Red

to this:

return (theTWAValue < 85) ? Color.Green :
    (theTWAValue < 90) ? Color.Yellow :
    Color.Red;

See, what's happening is if it parses you're not evaluating all three scenarios. It's okay to have the default return Color.Green, so leave that there, but if it parses and it's >= 0 then you need to evaluate all three conditions.

EDIT

As stated by Kevin DeVoe, in this case, since you're always getting Green, it appears the value is never actually parsing to an int. The code inside the if was still wrong because it didn't have all three scenarios, but your root problem is the fact that it's not parsing - ever.

于 2013-07-17T19:11:56.057 回答
0

textnull在所有情况下都是。

尝试更换:

ForeColor='<%# GetColorForLabel( Eval("TWA") as string ) %>'

和:

ForeColor='<%# GetColorForLabel( "" + Eval("TWA") ) %>'

或者你可以使用.ToString(),但我真的觉得它更复杂......接受object作为输入GetColorForLabel()也可以。

注意:您一直在做相当于:

string s = (object)5 as string;  // always going to be null
于 2013-07-18T01:30:02.370 回答
0

你的问题出在

if (text != null && int.TryParse(text, out theTWAValue) && theTWAValue >= 0)

您需要调试它并逐步检查它以找出它失败的原因。在 if 语句之前添加它并检查控制台。

if(text == null) 
     Console.WriteLine("Text is null");
if(!int.TryParse(text, out theTWAValue)) 
     Console.WriteLine("Parse Failed");
Console.WriteLine("TWA Value: " + theTWAValue);
于 2013-07-17T19:22:33.127 回答