0

I have the following VB code in an asp.net page.

It works fine, however I am sure there must be a better way (one only line perhaps) to cover all 6 fields within the block.

As you can see there are 4 rows of fields, each row contains 6 fields. Their ID all start with prefix "txtDO", then a number (7 to 11), then a suffix (_D, _C, _1, _2, _3, _B)

Note that these are not the only fields on the page, there are several groups. Since I have also additional code running within the VB loop, I need to keep it this way.

For l = 7 To 11                   
                CType(FindControl("txtDO" & l & "_D"), TextBox).Style.Add("border-bottom", "thin dotted #AD9F9F")
                CType(FindControl("txtDO" & l & "_C"), TextBox).Style.Add("border-bottom", "thin dotted #AD9F9F")
                CType(FindControl("txtDO" & l & "_1"), TextBox).Style.Add("border-bottom", "thin dotted #AD9F9F")
                CType(FindControl("txtDO" & l & "_2"), TextBox).Style.Add("border-bottom", "thin dotted #AD9F9F")
                CType(FindControl("txtDO" & l & "_3"), TextBox).Style.Add("border-bottom", "thin dotted #AD9F9F")
                CType(FindControl("txtDO" & l & "_B"), TextBox).Style.Add("border-bottom", "thin dotted #AD9F9F")
            Next l

I am looking for something like this, see the suffix * which covers ALL fields, therefore only one line of code instead of 6.

CType(FindControl("txtDO" & l & "*"), TextBox).Style.Add("border-bottom", "thin dotted #AD9F9F")

Is this possible ?

zed
  • 2,298
  • 4
  • 27
  • 44
user1135218
  • 393
  • 1
  • 8
  • 26

2 Answers2

3

Why don't you use a nested Foreach loop...

Dim letters() As String = {"_D", "_C", "_1", "_2", "_3", "_B"}
For l = 7 To 11 
    For Each letter As String In letters                   
        CType(FindControl("txtDO" & l & letter).Style.Add("border-bottom", "thin dotted #AD9F9F")
    Next
Next l
Damiano C.
  • 282
  • 1
  • 9
  • Sorry, I didn't use a nested foreach loop as I didn't know I could do that. Now I do. Fantastic!, although not the code I was after, this shortens my routine by at least 50% and makes the whole thing simpler and more logical. By the way, your code gave an error, but this works (was missing the, Textbox) part: CType(FindControl("txtDO" & l & letter), TextBox).Style.Add("border-bottom", "thin dotted #AD9F9F") – user1135218 May 22 '15 at 13:12
0

In short, you can't do it with one line of code, with one FindControl like you are asking.
FindControl returns only one control to work with.
See MSDN documentation for more details.

zed
  • 2,298
  • 4
  • 27
  • 44