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)
我有一个带有一些vb代码的asp.net。我需要遍历许多文本框并基于其中一个框上的值,使字段可见(或其他)
Each "row" of fields has 6 fields related, I have used a prefix and sufix to differentiate between them.
每个“行”字段有6个字段相关,我使用前缀和sufix来区分它们。
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:
我总共有20套(每套6个字段)。所以我想做如下(标准VB),但我不知道如何在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.
任何帮助将不胜感激。
3 个解决方案
#1
You can do what you are asking but with FindControl and concatenating it properly.
您可以使用FindControl并正确连接它来执行您要求的操作。
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.
如果找不到控件,请将它们放在占位符中,并在占位符对象上调用FindControl。
#2
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.
但您也可以创建一个面板并将所有这些字段放在其中的同一行中。通过这样做,您可以使面板可见或不可见。
#3
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.
对于您而言,您还可以使用foreach循环进行控制,然后执行检查并隐藏或显示相应的选项。
#1
You can do what you are asking but with FindControl and concatenating it properly.
您可以使用FindControl并正确连接它来执行您要求的操作。
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.
如果找不到控件,请将它们放在占位符中,并在占位符对象上调用FindControl。
#2
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.
但您也可以创建一个面板并将所有这些字段放在其中的同一行中。通过这样做,您可以使面板可见或不可见。
#3
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.
对于您而言,您还可以使用foreach循环进行控制,然后执行检查并隐藏或显示相应的选项。