检查空值的字段值

问题描述:

我一直在研究这一段时间,并且我错过了显而易见的内容。我试图确保如果文本框的值为空白,则不会引发错误,而是在文本框中显示一条简单的消息。然而,我所得到的以及其他几种我尝试类似的方法都没有奏效。我不明白为什么这不起作用,我需要做不同的事情。检查空值的字段值

基础:

这是一个简单的计算器,它允许一个进入他们的腰部,颈部和高度测量,计算其预计的身体脂肪百分比在公式中使用。计算正常工作,除非字段留空。

感谢您的帮助!

我的代码:

 if (TBWaist.Text == null || TBNeck.Text == null || TBHeight.Text == null) 
     { 
      TBBodyFat.Text = "Value missing"; 
     } 
     else 
      if (TBWaist.Text != null & TBNeck.Text != null & TBHeight.Text != null) 
      { 
       double waist; 
       double neck; 
       double height; 

       waist = Convert.ToDouble(TBWaist.Text); 
       neck = Convert.ToDouble(TBNeck.Text); 
       height = Convert.ToDouble(TBHeight.Text); 

       TBBodyFat.Text = Convert.ToString(String.Format("{0:p2}", ((501.5/(1.0324 - .19077 * (Math.Log10(waist - neck)) + .15456 * (Math.Log10(height))) - 450)/100))); 

错误消息

这是,如果我离开腰部文本框为空的错误消息我得到。如果我留下其他任何空白,我也会得到同样的错误。

输入字符串的不正确的格式上线45

waist = Convert.ToDouble(TBWaist.Text); 

如何使用RequiredFieldValidator

或者,您可以检查该值是一个空字符串和/或空

if (
    String.IsNullOrEmpty(TBWaist.Text) || 
    String.IsNullOrEmpty(TBNeck.Text) || 
    String.IsNullOrEmpty(TBHeight.Text) 
) 

尝试像

if(String.IsNullOrEmpty(TBWaist.Text) == true) 
{ 
    // error case 
} 
else 
{ 
    // do stuff 
} 
+1

我们为什么要将“bool”与true进行比较? – 2010-08-08 22:45:39

+0

@Steven:这只是我们的编码标准,因为任何人都可以直接看到您比较哪个值。这不是真的需要。 – Viper 2010-08-09 08:45:06

+0

你很可能会被这个标准所约束,但客观来说,这不是很好,因为它是多余的和任意的。该变量已经是一个布尔值。通过这个逻辑,你应该通过再次将它与'true'进行比较来检验比较结果,并且'无限'。:-) – 2010-08-09 13:40:33

您应该使用String.IsNullOrEmpty(),而不是一个比较。您的输入字段值不为空,只是空的,因此转换失败。

protected void Button1_Click(object sender, EventArgs e) 
{ 
    //Navy Men's Body Fat Formula: %Fat=495/(1.0324-.19077(log(abdomen-neck))+.15456(log(height)))-450 
    //string outputString = String.Format("At loop position {0}.\n", i); 


    if (String.IsNullOrEmpty(TBWaist.Text) || String.IsNullOrEmpty(TBNeck.Text) || String.IsNullOrEmpty(TBHeight.Text)) 
    { 
     TBBodyFat.Text = "Value missing"; 
    } 
    else 
    { 
     double waist; 
     double neck; 
     double height; 

     waist = Convert.ToDouble(TBWaist.Text); 
     neck = Convert.ToDouble(TBNeck.Text); 
     height = Convert.ToDouble(TBHeight.Text); 

     TBBodyFat.Text = Convert.ToString(String.Format("{0:p2}", ((501.5/(1.0324 - .19077 * (Math.Log10(waist - neck)) + .15456 * (Math.Log10(height))) - 450)/100))); 
    } 

} 

您应该测试比你要测试的空以及

使用String.IsNullOrEmpty(TBWaist.Text)只是空值以上;

我会推荐使用TryParse方法。通过这种方式,空白,空字符串或不能转换为字符串的东西都是相同的。其次,我会建议在每个文本框中添加RequiredFieldValidators和/或RegularExpressionValidators,以确保用户输入一个值并且该值是数字。通过这种方式,您的事件过程中的检查将用作最后一次检查而不是要求回发验证

protected void Button1_Click(object sender, EventArgs e) 
{ 
    //Navy Men's Body Fat Formula: %Fat=495/(1.0324-.19077(log(abdomen-neck))+.15456(log(height)))-450 
    //string outputString = String.Format("At loop position {0}.\n", i); 

    double waist; 
    double neck; 
    double height; 

    if (!double.TryParse(TBWaist.Text, out waist) 
     || !double.TryParse(TBNeck.Text, out neck) 
     || !double.TryParse(TBHeight.Text, out height)) 
    { 
     ErrorMessageLabel.Text = "Please ensure that each value entered is numeric."; 
     return; 
    } 

    var bodyFat = (501.5 
     /(1.0324 - .19077 * (Math.Log10(waist - neck)) 
      + .15456 * (Math.Log10(height)) 
      ) - 450)/100; 

    TBBodyFat.Text = bodyFat.ToString("P2"); 
} 
+0

这是最全面的答案。我可能会建议的唯一的事情就是他们在'TryParse'之前的'Text'上调用'Trim'。 – 2010-08-08 22:46:18

+2

@Steven Sudit - TryParse不需要修剪。 ''12345.6789“'和12345.6789”'以及'“12345.6789”'一样可以被解析。 – Thomas 2010-08-08 22:52:57

+0

当然你是对的。 – 2010-08-09 04:21:27