循环通过文本框和标签

时间:2013-04-23 08:16:23

标签: asp.net vb.net testing loops

我正在使用Microsoft visual studios 2012在VB中进行ASP.NET在线测试。

我试图让循环通过我的文本框并根据一个单词检查它们将更改为针对数据库进行验证以查看答案是否正确但是当我执行循环时我无法从文本框中获取文本

请参阅下文

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

2 个答案:

答案 0 :(得分: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

即可

<强>更新

Page.FindControl仅适用于页面上根元素的直接子元素的控件。相反,您应该尝试递归调用FindControl。您还应该确保存在标识为TextQ1的控件 - 查看客户端页面的HTML源代码,以确保存在具有此ID的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控件执行相同的操作。

答案 1 :(得分:0)

看起来您只使用名称而不是文本框尝试使用下面的代码

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