2016-08-04 28 views
1

我有總分的文本框。 我需要禁用驗證其他具有讀值的文本框。如何檢查大於零的文本框控件值?

我需要檢查的條件是如果總分大於零和null或empty.here是code.I試圖把(!string.IsNullOrEmpty(txtTotalScore.Text))&& (txtTotalScore.Text>0) 它沒有工作,因爲txtscore是文本框控制和0是整數。 我該如何解決這個問題?

TextBox myscore = fv.FindControl("txtTotalScore") as TextBox; 
if (!string.IsNullOrEmpty(txtTotalScore.Text))      
    RangeValidator rv = fv.FindControl("rngReading") as RangeValidator; 
    rv.Enabled = false; 
} 

回答

1

爲了將文本框的內容與整數進行比較,您需要將內容作爲數字進行分析(即「42」= 42)。您可以通過使用Parse()TryParse()方法,然後那結果比較爲0

if (!string.IsNullOrEmpty(txtTotalScore.Text))  
    // At this point, you know it isn't null 
    var potentialValue = -1; 
    // Parse the textbox and store the value in potentialValue 
    Int32.TryParse(txtTotalScore.Text, out potentialValue); 
    if(potentialValue > 0) 
    { 
      // Then disable your range validator 
      RangeValidator rv = fv.FindControl("rngReading") as RangeValidator; 
      rv.Enabled = false; 
    } 
} 
1

我文本框的文本只是轉換爲int這樣做,然後做積極的檢查:

TextBox myScore = fv.FindControl("txtTotalScore") as TextBox; 
try 
{ 
    int totalScore = Convert.ToInt32(myScore.Text); 
    if (totalScore > 0) 
    { 
     RangeValidator rv = fv.FindControl("rngReading") as RangeValidator; 
     rv.Enabled = false; 
    } 
} 
catch(FormatException ex) 
{ 
    // Show error message stating that text should be a numeric value 
} 
相關問題