2012-04-08 56 views
1

我曾經有過這方面的工作是這樣....asp.net vb.net爲什麼這個IF不工作?

Dim AnnEnt As Label = FormView1.FindControl("Holiday_RemainingLabel") 

    txtNoofDays.Text.ToString() 
    AnnEnt.Text.ToString() 

    If txtNoofDays.Text >= AnnEnt.Text Then 

     lblHolRequestResponse.Text = "Your holiday could not be saved" 
    Else 

我最近改成這樣,它不再起作用

Dim remain As TextBox = FormView1.FindControl("Holiday_RemainingTextBox") 



    txtNoofDays.Text.ToString() 
    remain.Text.ToString() 

    If txtNoofDays.Text >= remain.Text Then 

     lblHolRequestResponse.Text = "Your holiday could not be saved" 

    Else 

是什麼在文本框中的區別formview和標籤在formview中,以防止這種工作?

因爲我已經試過......

Dim days = txtNoofDays.Text 

    days.ToString() 
    AnnEnt.Text.ToString() 
    remain.Text.ToString() 
    If remain.Text.ToString < days.ToString Then 
     lblHolRequestResponse.Text = "Your holiday could not be saved" 
+2

我不明白你的代碼。 'txtNoofDays.Text.ToString()'基本上什麼都不做,因爲字符串沒有分配給任何東西。 'txtNoofDays.Text> = remain.Text'將按字母順序比較兩個字符串(根據排序順序)。你確定你想這麼做嗎? – 2012-04-08 21:48:49

+0

他們都是數字。我只是想讓它比較數字2和數字1. ..如果數字1比數字2高,然後顯示消息。我嘗試了幾種不同的方式,但似乎沒有工作 – user1055487 2012-04-08 21:52:10

+0

不工作如何?空引用錯誤,因爲它找不到名爲「保留」的文本框? NoOfDays看起來對我來說可能是一個數字,當您比較字符串「2」>「10」時... – 2012-04-08 21:53:18

回答

3

如果要比較字符串的數字,將它們轉換成數字。

例如(asssuming他們ints):

Dim remain As TextBox = FormView1.FindControl("Holiday_RemainingTextBox") 
Dim remaining = Int32.Parse(remain.Text) 
Dim numOfDays = Int32.Parse(txtNoofDays.Text) 

If numOfDays >= remaining Then 
    lblHolRequestResponse.Text = "Your holiday could not be saved" 
End If 

Int32.Parse Method

否則你按字母順序進行比較。

String.CompareTo Method

+0

+1;此外,「你的假期無法保存」 - 糟糕的假期! – dash 2012-04-08 22:00:33