2013-04-23 150 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