0

my problem is:

I have a webpage that looks kinda like this:

     <asp:PlaceHolder ID="Formular" runat="server">
<table>
<tr runat="server" visible="true" id="1">
<td>  <asp:TextBox ID="TextBox13" runat="server" AutoPostBack="true" OnTextChanged="tb_Changed" CssClass="tx"></asp:TextBox>
</td>
</table>
    </asp:PlaceHolder>

Now i want loop through all controls on webpage but of course, i can't access that TextBox13 with this code:

Dim tb as TextBox
For Each ctrl In Formular.Controls
            If TypeOf ctrl Is TextBox Then
                tb = ctrl
                If tb.Text.Trim.Length = 0 Then
                    tb.Style("background-color") = "red"
                    count += count + 1
                Else
                    tb.Style("background-color") = "white"
                End If
            End If
        Next

Is there any elegant easy way to access that textbox?

Reason why i'm not hiding that table row with javascript is because code of this page will be used later somewhere else and it will be much easier w/o any javascripts.

L.Johnson
  • 79
  • 2
  • 11

1 Answers1

0

You can access the object of the text box by:

Dim szTextbox As String = Left(Request.Form("TextBox13"), 50) to get the content as string

TextBox13.Text to get and set string in textbox control

Update

Based on finding the specific textbox.

Recursively go through page and find textbox based on id:

Public Function FindControlRecursive(Of ItemType)(ByVal Ctrl As Object, ByVal id As String) As ItemType
     If String.Compare(Ctrl.ID, id, StringComparison.OrdinalIgnoreCase) = 0 AndAlso TypeOf Ctrl Is ItemType Then
          Return CType(Ctrl, ItemType)
     End If

     For Each c As Control In Ctrl.Controls
          Dim t As ItemType = FindControlRecursive(Of ItemType)(c, id)

          If t IsNot Nothing Then
               Return t
          End If
     Next

     Return Nothing
End Function

Code from: Looping through textboxes and labels

Zaren Wienclaw
  • 181
  • 4
  • 15
  • Thanks, that is elegant and easy way... but not imagine i have like 10 situations like this on one page with many textboxes inside. – L.Johnson Aug 30 '18 at 05:21