简体   繁体   中英

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.

You can do what you are asking but with F indControl 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.

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.

第三个选择是,您还可以对控件使用foreach循环,然后执行检查并相应地隐藏或显示。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM