user1135218
user1135218

Reputation: 403

looping through asp.net textboxes visual basic

I have an asp.net with some vb code behind. I need to loop through many textboxes and based on a value on one of the boxes, make the field visible (or whatever)

Each "row" of fields has 6 fields related, I have used a prefix and sufix to differentiate between them.

If txtOC7_D.Text <> "" Then
                    txtOC7_D.Enabled = True
                    txtOC7_C.Enabled = True
                    txtOC7_1.Enabled = True
                    txtOC7_2.Enabled = True
                    txtOC7_3.Enabled = True
                    txtOC7_B.Enabled = True
                ElseIf txtOC7_D.Text = "" Then
                    txtOC7_D.Enabled = False
                    txtOC7_C.Enabled = False
                    txtOC7_1.Enabled = False
                    txtOC7_2.Enabled = False
                    txtOC7_3.Enabled = False
                    txtOC7_B.Enabled = False
                End If
                If txtOC8_D.Text <> "" Then
                    txtOC8_D.Enabled = True
                    txtOC8_C.Enabled = True
                    txtOC8_1.Enabled = True
                    txtOC8_2.Enabled = True
                    txtOC8_3.Enabled = True
                    txtOC8_B.Enabled = True
                ElseIf txtOC8_D.Text = "" Then
                    txtOC8_D.Enabled = False
                    txtOC8_C.Enabled = False
                    txtOC8_1.Enabled = False
                    txtOC8_2.Enabled = False
                    txtOC8_3.Enabled = False
                    txtOC8_B.Enabled = False
                End If

I have a total of 20 sets (of 6 fields each). So I would like to do as follows (standard VB) but I can't see how to do it on ASP.NET VB:

for l=1 to 20
 If Controls("txtOC" & l) & "_D"<>"" Then
   Controls("txtOC" & l) & "_D".visible=true
   Controls("txtOC" & l) & "_C".visible=true
   Controls("txtOC" & l) & "_1".visible=true
   Controls("txtOC" & l) & "_2".visible=true
   Controls("txtOC" & l) & "_3".visible=true
   Controls("txtOC" & l) & "_B".visible=true
 else
   Controls("txtOC" & l) & "_D".visible=false
   Controls("txtOC" & l) & "_C".visible=false
   Controls("txtOC" & l) & "_1".visible=false
   Controls("txtOC" & l) & "_2".visible=false
   Controls("txtOC" & l) & "_3".visible=false
   Controls("txtOC" & l) & "_B".visible=false
 end if
next l

Any help will be appreciated.

Upvotes: 0

Views: 212

Answers (3)

MaCron
MaCron

Reputation: 121

And a third option for you, you could also use a foreach loop for the controls then perform your check and hide or show accordingly.

Upvotes: 0

Ramon
Ramon

Reputation: 3

You should look for Reflection Methods.

But you can also create a panel and put all those fields from the same row within it. By doing that, you can make the panel visible or not.

Upvotes: 0

the_lotus
the_lotus

Reputation: 12748

You can do what you are asking but with FindControl and concatenating it properly.

If CType(FindControl("txtOC" & l & "_D"), TextBox).Text <>"" Then
   CType(FindControl("txtOC" & l & "_D"), TextBox).Visible=true

If you have trouble finding the control, put them in a placeholder and call the FindControl on the place holder object.

Upvotes: 1

Related Questions