0

我正在使用 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
4

2 回答 2

1

看起来您正在尝试使用控件的字符串标识符来代替实际的控件。相反,您应该使用此标识符并搜索页面上的实际控件。您可以使用FindControl 方法执行此操作

因此,您的函数看起来像(未经编译测试):

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,因为这是您的循环计数变量。

是否使用+运算符或&运算符进行字符串连接取决于您。这里已经有一个很好的问题和几个答案

您也不需要循环的退出条件 - 一旦达到 MaxTime,循环就会退出。如果您希望它提前退出,只需改变您的To条件(例如Currentloop To MaxTime - 1

更新:

Page.FindControl 仅适用于作为页面上根元素的直接子级的控件。相反,您应该尝试递归调用 FindControl。您还应该确保TextQ1存在具有 id 的控件 - 在客户端上查看页面的 HTML 源代码以确保存在TextBox具有此 id 的控件。

网上有很多这样的例子。这是您可以添加到页面的 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 控件执行相同的操作。

于 2013-04-23T08:33:12.737 回答
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
于 2013-04-23T09:11:16.940 回答