遍历文本框和标签 [英] Looping through textboxes and labels

查看:79
本文介绍了遍历文本框和标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Microsoft Visual Studios 2012在VB中的ASP.NET中进行此项目的在线测试.

Im doing this project for an online test in ASP.NET in VB im using Microsoft visual studios 2012.

我正在尝试使循环遍历文本框并针对一个单词检查它们,这将更改为针对数据库进行验证以查看答案是否正确,但是当我执行循环时,无法从文本框获取文本

Im trying to get a loop going through my textboxes and check them against a word this will be change to be validated against a database to see if the answer are correct but when I do my loop I cannot get my text from the textbox.

请参见下面

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

推荐答案

您似乎正在尝试使用控件的字符串标识符代替实际的控件.相反,您应该使用此标识符并在页面上搜索实际的控件.您可以使用

It looks like you are trying to use the string identifier of the control in place of the the actual control. Instead, you should take this identifier and search for the actual control on the page. You can do this using the FindControl method

因此,您的函数应类似于(未经编译测试):

Your function would therefore look something like (not compile tested):

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属性即可.该集合特别重要,因为您要更新原始控件属性,以便在页面上显示它.

You don't need all of those variables. Instead, just find the actual controls, and interact with the properties directly - just check the TextBox.Text value when you need to, and set the Label.text property directly. The set is especially important as you want to update the original control property so it is shown on the page.

类似地,您不需要Number-您可以使用check,因为这是您的循环计数变量.

Similarly, you don't need Number - you can use check as this is your loop counting variable.

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

Whether you use the + operator or the & operator for string concatenation is up to you. There's already a good question and several answers here.

您也不需要循环的退出条件-达到MaxTime后,循环将立即退出.如果您希望它提早退出,只需更改您的To条件(例如Currentloop To MaxTime - 1)

You also don't need the exit condition for the loop - the loop will exit as soon as you reach MaxTime. If you want it to exit early, just vary your To condition (e.g. Currentloop To MaxTime - 1)

更新:

Page.FindControl将仅与作为页面上根元素的直接子代的控件一起使用.相反,您应该尝试递归调用FindControl.您还应确保存在ID为TextQ1的控件-在HTML源代码中查找客户端页面的页面,以确保存在具有此ID的TextBox.

Page.FindControl will only work with controls that are immediate children of the root element on the page. Instead, you should try calling FindControl recursively. You should also make sure that a control with the id TextQ1 exists - look in the HTML source for the page on the client to make sure a TextBox with this id exists.

网上有很多这样的例子.这是VB.Net版本(来源: http://www .pavey.me/2007/09/recursive-pagefindcontrol-for-vbnet.html ),您可以将其添加到页面中:

There are many examples of this on the net. Here's a VB.Net version (source: http://www.pavey.me/2007/09/recursive-pagefindcontrol-for-vbnet.html) that you can add to your page:

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

您在上面代码中的行将变为:

Your line in the code above would then become:

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

您还需要对Label控件执行相同的操作.

You'd also need to do the same for the Label control.

这篇关于遍历文本框和标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆