2013-04-23 104 views
0

即時通訊使用Microsoft Visual工作室2012年做這個項目的一個在線測試在ASP.NET VB中的IM循環通過文本框和標籤

我試着去得到一個循環通過我的文本框會,並檢查他們對一個單詞本將被更改爲針對數據庫進行驗證以查看答案是否正確,但是當我執行循環時,我無法從文本框中獲取我的文本。

請參考下面

Private Sub GoGoGo() 

    Dim Textboxname As String  ' 
    Dim textbox As Object 
    Dim TextboxText As Object 
    Dim Labelname As String 
    Dim label As Object 
    Dim LabelText As Object 
    Dim Number As Integer = 1 
    Dim MaxTime As Integer = 9 
    Dim Currentloop As Integer = 1 



    For check As Integer = Currentloop To MaxTime 


     If Currentloop <= MaxTime Then 
      Textboxname = "TextQ" + Number 
      textbox = Textboxname 
      TextboxText = textbox 
      textbox.ReadOnly = True 

     End If 

     If Currentloop <= MaxTime Then 
      Labelname = "Label" + Number 
      label = Labelname 
      LabelText = label.Text 
      label.Visible = True 

     End If 

     Number = Number + 1 



     If TextboxText = "" Then 
      label.Text = "no imput" 
      label.ForeColor = Drawing.Color.Black 

     End If 

     If TextboxText = "server" Then 
      label.Text = "Correct" 
      label.ForeColor = Drawing.Color.Green 
     End If 

     If TextboxText = "Wrong" Then 
      label.Text = "Wrong" 
      label.ForeColor = Drawing.Color.Red 
     End If 


     If check = 9 Then 
      Exit For 
     End If 


    Next 

End Sub 

回答

1

看起來你要使用該控件的字符串標識符來代替實際控制人。相反,您應該使用此標識符並搜索頁面上的實際控件。您可以使用FindControl method

你的功能,因此看起來是這樣的(未測試編譯)做到這一點:

Private Sub GoGoGo() 
    ' 
    Dim oTextBox As TextBox 
    Dim oLabel As Label 

    Dim MaxTime As Integer = 9 
    Dim Currentloop As Integer = 1 

    For check As Integer = Currentloop To MaxTime 

     If Currentloop <= MaxTime Then 
      'NB You may have to use a recursive call to FindControl. See below. 
      oTextBox = CType(Page.FindControl("TextQ" & CStr(check)), TextBox) 
      OTextBox.ReadOnly = True; 
     End If 

     If Currentloop <= MaxTime Then 
      'NB You may have to use a recursive call to FindControl. See below. 
      oLabel = CType(Page.FindControl("Label" & CStr(check)), Label) 
      oLabel.Visible = True 
     End If 

     If oTextBox.Text = "" Then 
      oLabel.Text = "no imput" 
      oLabel.ForeColor = Drawing.Color.Black 

     End If 

     If oTextBox.Text = "server" Then 
      oLabel.Text = "Correct" 
      oLabel.ForeColor = Drawing.Color.Green 
     End If 

     If oTextBox.Text = "Wrong" Then 
      oLabel.Text = "Wrong" 
      oLabel.ForeColor = Drawing.Color.Red 
     End If 

    Next 

End Sub 

一些注意事項:

你並不需要所有這些變量。相反,只需找到實際的控件,並直接與屬性進行交互 - 只需在需要時檢查TextBox.Text值,並直接設置Label.text屬性即可。該集合非常重要,因爲您要更新原始控件屬性,以便它顯示在頁面上。

同樣,你不需要Number - 你可以使用check,因爲這是你的循環計數變量。

是否使用+運算符或&運算符進行字符串連接取決於您。已經有good question and several answers here

您也不需要循環的退出條件 - 只要達到MaxTime,循環就會退出。如果你想讓它提前退出,只是改變你的To條件(例如Currentloop To MaxTime - 1

UPDATE:

Page.FindControl將只與在頁面上的根元素的直接子控制工作。相反,你應該嘗試遞歸調用FindControl。您還應該確保存在編號爲TextQ1的控件 - 查看客戶端頁面的HTML源代碼,以確保存在具有此編號的TextBox

網上有很多這樣的例子。這裏有一個VB.Net版本(來源:http://www.pavey.me/2007/09/recursive-pagefindcontrol-for-vbnet.html),您可以添加到您的網頁:

Public Function FindControlRecursive(Of ItemType)(ByVal Ctrl As Object, ByVal id As String) As ItemType 
    If String.Compare(Ctrl.ID, id, StringComparison.OrdinalIgnoreCase) = 0 AndAlso TypeOf Ctrl Is ItemType Then 
      Return CType(Ctrl, ItemType) 
    End If 

    For Each c As Control In Ctrl.Controls 
      Dim t As ItemType = FindControlRecursive(Of ItemType)(c, id) 

      If t IsNot Nothing Then 
       Return t 
      End If 
    Next 

    Return Nothing 
End Function 

你的代碼行上面會再變成:

oTextBox = FindControlRecursive(of TextBox)(Page.Controls(0), "TextQ" & CStr(check))

您還需要會爲Label控件做同樣的事情。

+0

喜短跑代碼試試,我剛纔想了這一點,在oTextBox = CTYPE(頁。FindControl(「TextQ」+ Number),TextBox)即時獲取錯誤「從字符串」TextQ「轉換爲鍵入」Double「無效。我該怎麼做,並感謝您的幫助 – 2013-04-23 08:44:56

+0

@BennjaminMiles自第一次回答以來,我已經稍微改變了答案。讓我知道你是怎麼辦的。然而,爲了解釋,'oTextBox = CType(Page.FindControl(「TextQ」&check),TextBox)'可能更合適。 – dash 2013-04-23 08:46:37

+0

嗨破折號,我現在已經改變了代碼來檢查,因爲你已經說過了,但是這仍然會出現錯誤「從字符串」TextQ「轉換爲類型」Double「無效。親切的問候Benjamin Miles – 2013-04-23 09:01:37

0

看起來像你正在使用文本框的唯一名字istead下面

Private Sub GoGoGo() 

    Dim Textboxname As String  ' 
    Dim textbox As TextBox 
    Dim TextboxText As Object 
    Dim Labelname As String 
    Dim label As Object 
    Dim LabelText As Object 
    Dim Number As Integer = 1 
    Dim MaxTime As Integer = 9 
    Dim Currentloop As Integer = 1 



For check As Integer = Currentloop To MaxTime 


    If Currentloop <= MaxTime Then 
     Textboxname = "TextQ" + Number 
     textbox = Ctype(Me.Controls(Textboxname), TextBox) 
     TextboxText = textbox.Text 
     textbox.ReadOnly = True 

    End If 

    If Currentloop <= MaxTime Then 
     Labelname = "Label" + Number 
     label = Labelname 
     LabelText = label.Text 
     label.Visible = True 

    End If 

    Number = Number + 1 



    If TextboxText = "" Then 
     label.Text = "no imput" 
     label.ForeColor = Drawing.Color.Black 

    End If 

    If TextboxText = "server" Then 
     label.Text = "Correct" 
     label.ForeColor = Drawing.Color.Green 
    End If 

    If TextboxText = "Wrong" Then 
     label.Text = "Wrong" 
     label.ForeColor = Drawing.Color.Red 
    End If 


    If check = 9 Then 
     Exit For 
    End If 


Next 

End Sub 
+0

你好Mandeep,我試過這個和「textbox = Ctype(Me.Controls(Textboxname),TextBox) 」我得到這個錯誤,請指教從字符串「TextQ1」轉換爲類型'整數'是無效的。 – 2013-04-23 09:30:36

+0

嘗試textbox = Ctype(Me.Controls(key:= Textboxname),TextBox) – 2013-04-23 09:42:58