Click here to Skip to main content
15,031,193 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
VB
TextBox1.Text = String.Empty
VB
TextBox1.Text.Trim.Length = 0
VB
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:
VB
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
Posted
Updated 8-Dec-13 20:38pm
v2
Comments
Maciej Los 9-Dec-13 2:45am
   
GridView control is a set of cells...

What you mean: "it executes all the If-Else Statement instead of just executing the If Statement alone"? Do you want to return the collection of empty "textboxes" or do you want to catch first empty cell?
Irish Angel 9-Dec-13 2:54am
   
Yes. I have a ItemTemplate called txtAnswer. Using a For Loop, it scans all rows and catch a gridview row that has empty textbox. Nothing wrong goes there for me, the only thing that isn't working is the If-Else..
Maciej Los 9-Dec-13 2:55am
   
Please, see my updated comment.
Irish Angel 9-Dec-13 3:05am
   
Okay. I'll sight an example. I have a gridview with an ItemTemplate (Textbox) called txtAnswer. These Textbox should always have a text on it. If there is a row with an empty textbox, it should returns the row index (which works well). When there is no row with an empty textbox, it should just do something like popping a messagebox. My problem is when there is an empty textbox, it returns the gridview row index and also pop out a messagebox.
Karthik_Mahalingam 9-Dec-13 3:48am
   
assigning a variable inside a loop is a big mistake..

what for the below statement ??
Exit For -- > exists the for loop if it matches the condition , and it wont check for the remaining items.
Exit Sub -- > comes out of the method. after that nothing will execute in the method..

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 tried String.IsNullOrEmpty(TextBox1.Text) or String.IsNullOrWhiteSpace(TextBox1.Text) for your Textbox is empty Checks? ^^ (the Names are self-explanatory)
looks nicer than
TextBox1.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:
VB
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[^].
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900