[英]Looping through textboxes and labels
Im doing this project for an online test in ASP.NET in VB im using Microsoft visual studios 2012. 我正在使用Microsoft Visual Studios 2012在VB中的ASP.NET中进行此项目的在线测试。
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. 我试图让循环遍历我的文本框并根据一个单词检查它们,这将更改为针对数据库进行验证,以查看答案是否正确,但是当我执行循环时,无法从文本框获取文本。
Please see below 请看下面
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
您可以使用FindControl方法执行此操作
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
Some notes: 一些注意事项:
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.
相反,只需找到实际控件,然后直接与属性进行交互-只需在需要时检查TextBox.Text值,然后直接设置Label.text属性即可。 The set is especially important as you want to update the original control property so it is shown on the page.
当您要更新原始控件属性时,该集尤为重要,因此它会显示在页面上。
Similarly, you don't need Number
- you can use check
as this is your loop counting variable. 同样,您不需要
Number
-您可以使用check
因为这是您的循环计数变量。
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 . 这里已经有一个很好的问题和几个答案 。
You also don't need the exit condition for the loop - the loop will exit as soon as you reach MaxTime. 您也不需要循环的退出条件-达到MaxTime后,循环将立即退出。 If you want it to exit early, just vary your
To
condition (eg Currentloop To MaxTime - 1
) 如果您希望它提早退出,只需更改您的
To
条件(例如Currentloop To MaxTime - 1
)
UPDATE: 更新:
Page.FindControl will only work with controls that are immediate children of the root element on the page. Page.FindControl将仅与作为页面上根元素的直接子代的控件一起使用。 Instead, you should try calling FindControl recursively.
相反,您应该尝试递归调用FindControl。 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. 您还应确保存在ID为
TextQ1
的控件-在HTML源代码中查找客户端页面的内容,以确保存在具有此ID的TextBox
。
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:
您可以将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
Your line in the code above would then become: 上面代码中的行将变为:
oTextBox = FindControlRecursive(of TextBox)(Page.Controls(0), "TextQ" & CStr(check))
You'd also need to do the same for the Label control. 您还需要对Label控件执行相同的操作。
It Look like you are using only name istead of textbox try with the code below 看起来您仅使用名称而不是文本框尝试以下代码
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.