我在VB.Net的循环中遇到了If-Else语句的问题 [英] I am having issues with If-Else Statement inside a loop in VB.Net
问题描述
我使用 For Loop
来扫描 Gridview
中的文本框。条件是如果有一个空文本框,它将获得具有空文本框的Gridview Row的索引。如果没有空文本框,那么它将执行其他操作。这里的问题是当我试图故意留下一个空文本框时,它执行所有If-Else语句而不是仅执行If语句。我试过
TextBox1.Text = String .Empty
TextBox1.Text。 Trim.Length = 0
TextBox1.Text =
但它仍无效。除了我上面提到的还有其他方法,比如使用 For Each
扫描Gridview行并捕获空文本框然后使用 Select Case
?怎么样?谢谢。
无论如何,到目前为止,我有这些代码:
对于 i 作为 整数 = 0 Gridview1.Rows.Count - 1 步骤 i + 1
Dim TextBox1 As TextBox = DirectCast ( Me .Gridview1.Rows(i).FindControl ( txtAnswer),TextBox)
如果 TextBox1.Text.Trim.Length = 0 那么
会话(< span class =code-string> i)= i
退出 对于
退出 < span class =code-keyword> Sub
ElseIf TextBox1.Text.Trim.Length<> 0 然后
' < span class =code-comment>将在此处执行其他操作
结束 如果
下一步
该Messagebox代码是你的FOR-Construct下面呢? ..当然你退出for,所以Exit Sub将不会在第一个IF中被执行。
并且你试过String.IsNullOrEmpty(TextBox1.Text)
或String.IsNullOrWhiteSpace(TextBox1.Text)
您的文本框是否为空检查? ^^(名称不言自明)
看起来比TextBox1.Text.Trim.Length = 0;)
请先看看我的评论。我不确定你想要达到什么目标...
如果你想赶上第一个空行,请使用类似的东西:
Dim bIsEmptyTxtBox AS Boolean = false
Dim iRow 作为 整数 = -1 ' 行索引从0开始
对于 i 作为 < span class =code-keyword>整数 = 0 Gridview1.Rows.Count - 1
Dim TextBox1 As TextBox = DirectCast (我 .Gridview1.Rows(i).FindControl( txtAnswer),TextBox )
' 捕获第一个空行
如果 TextBox1.Text.Trim.Length = 0 那么
bIsEmptyTxtBox = True
iRow = i
退出 对于
结束 如果
下一步 i
如果 bIsEmptyTxtBox 那么
Session( i)= iRow
Else
' po p up消息框;)
结束 如果
I use For Loop
to scan the textboxes inside the Gridview
. The condition is if there is a textbox that is empty, it will get the index of the Gridview Row that has an empty textbox. If there is no textbox that is empty then it will do something else. The problem here is when I try to intentionally left an empty textbox, it executes all the If-Else Statement instead of just executing the If Statement alone. I tried
TextBox1.Text = String.Empty
TextBox1.Text.Trim.Length = 0
TextBox1.Text = ""
but it still not working. Is there any other method other than what I have mentioned above, like using For Each
to scan the Gridview row and catch empty textboxes then using Select Case
? How? Thanks.
Anyway, I have these code so far:
For i As Integer = 0 To Gridview1.Rows.Count - 1 Step i + 1
Dim TextBox1 As TextBox = DirectCast(Me.Gridview1.Rows(i).FindControl("txtAnswer"), TextBox)
If TextBox1.Text.Trim.Length = 0 Then
Session("i") = i
Exit For
Exit Sub
ElseIf TextBox1.Text.Trim.Length <> 0 Then
'will do something else here
End If
Next
Is that Messagebox Code the next under your FOR-Construct? .. course you exit the for, so that "Exit Sub" won't get executed in that first IF.
and have you triedString.IsNullOrEmpty(TextBox1.Text)
orString.IsNullOrWhiteSpace(TextBox1.Text)
for your Textbox is empty Checks? ^^ (the Names are self-explanatory)
looks nicer thanTextBox1.Text.Trim.Length = 0;)
Please, see my comment first. I'm not sure what you want to achieve...
If you want to catch first empty row, please use something like that:
Dim bIsEmptyTxtBox AS Boolean = false Dim iRow As Integer = -1 'index of row starts from 0 For i As Integer = 0 To Gridview1.Rows.Count - 1 Dim TextBox1 As TextBox = DirectCast(Me.Gridview1.Rows(i).FindControl("txtAnswer"), TextBox) 'catch first empty row If TextBox1.Text.Trim.Length = 0 Then bIsEmptyTxtBox = True iRow = i Exit For End if Next i If bIsEmptyTxtBox Then Session("i") = iRow Else 'pop up message box ;) End if
To list all empty rows, you should use array[^], list[^] or collection of gridview rows[^].
这篇关于我在VB.Net的循环中遇到了If-Else语句的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!