0

当我运行我的 C# 代码时,我收到此错误:输入字符串的格式不正确。它突出显示了这段代码:

       theTWAValue=Convert.ToInt32(theTWALabel.Text);

为什么会这样?

附加信息:

整个 C# 函数:

    <script  runat="server">
                protected void YourListView_Load(object sender, EventArgs e)
{
    Label theTWALabel;
 int theTWAValue;
foreach (ListViewItem item in YourListView.Items)
{
        theTWALabel = (Label)item.FindControl("TWALabel");
    theTWAValue = Convert.ToInt32(theTWALabel.Text);
    if (theTWAValue >= 0)
    {
        if (theTWAValue < 90)
            theTWALabel.ForeColor = System.Drawing.Color.Yellow;
        else
            theTWALabel.ForeColor = System.Drawing.Color.Red;
    }

}   
 }

                </script>

这是其余的代码:

<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") %>' />
          <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

5 回答 5

3

如果您的theTWALabel.Text值为空,那么您将收到此错误。在尝试转换之前检查内部是否有值。

于 2013-07-17T17:29:24.847 回答
2

您必须实际检查输入是否有效——我将使用TryParseConvert 函数代替:

if (int.TryParse(theTWALabel.Text, out theTWAValue))
{
}

更新后期编辑

我可能是错的,但我认为Label控件的值不会被发布回服务器——这可以解释为什么它们显示为空。在任何情况下,从“加载”事件中设置颜色似乎都不必要地复杂。为什么不创建一个辅助方法并直接绑定它呢?

protected 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;
}

然后用它来绑定 Foreground 属性:

<asp:Label id="TWALabel" runat="server" 
    Text = '<%# Eval("TWA") %>' 
    Foreground = '<%# GetColorForLabel( Eval("TWA") as string ) %>' />
于 2013-07-17T17:30:48.883 回答
1

你可以加Trim function

 theTWAValue=Convert.ToInt32(theTWALabel.Text.Trim());

msdn 链接:http: //msdn.microsoft.com/fr-fr/library/vstudio/t97s7bs3.aspx

我建议你使用Int32.TryParse function

链接:http: //msdn.microsoft.com/fr-fr/library/vstudio/f02979c7.aspx

于 2013-07-17T17:28:17.687 回答
1

尝试这个

theTWAValue = Convert.ToInt32(theTWALabel.Text.ToString().Trim());
于 2013-07-17T17:28:49.010 回答
1

请改用 int.TryParse。

int value;
if(!int.TryParse(theTWALabel.Text, out value))
{
  //didn't parse right
}
于 2013-07-17T17:31:05.920 回答