locked
Read clientID of each text box within a user control RRS feed

  • Question

  • User-1668014665 posted

    I have

    ASP NET 2, vb net, MSVS 2005

    I wish to read the name (or clientiD) of each text box with in a user control installed within a master page

    This is my attempt which does not work

            Public Shared Sub ScrapeTextBoxNames(ByVal p As Page)
    
           For Each ctlMaster As Control In p.Page.Controls
                    If TypeOf ctlMaster Is MasterPage Then
                        For Each ctlForm As Control In ctlMaster.Controls
                            If TypeOf ctlForm Is HtmlForm Then
                                For Each ctlContent As Control In ctlForm.Controls
                                    If TypeOf ctlContent Is ContentPlaceHolder Then
                                        For Each c As Control In ctlContent.Controls
                                            If TypeOf c Is System.Web.UI.WebControls.TextBox Then
                                                Dim txt As System.Web.UI.WebControls.TextBox = TryCast(c, System.Web.UI.WebControls.TextBox)
    
                                                'This prints to a text file out of site
                                                Util.zPFile("TBName: " & txt.ClientID.ToString)
    
                                            End If
                                        Next
                                    End If
                                Next
                            End If
                        Next
                    End If
                Next
    
    
            End Sub

    Any ideas how to do this?

    I guess I need to write code to read the usercontrol  and not the master page, I guess it matter where you execute the code.

    Tuesday, March 19, 2019 4:55 PM

Answers

  • User-1668014665 posted

    Solved

            'USE LIKE FindTextBoxNames FindTextBoxNames(Me.page.form)
            Public Shared Sub FindTextBoxNames(ByVal ctrl As Control)
                Dim txt As ArrayList = New ArrayList()
    
                For Each item As Control In ctrl.Controls
    
                    If item.[GetType]().ToString() = "System.Web.UI.WebControls.TextBox" Then
                        Dim txb As TextBox = CType(item, TextBox)
                        txt.Add(txb.ClientID.ToString)
                    End If
    
                    FindTextBoxNamesChild(item, txt)
                Next
    
    
                If txt.Count > 0 Then
                    For i As Integer = 0 To txt.Count - 1
                        Util.zPFile(txt(i).ToString)
                    Next
                End If
    
            End Sub
            Public Shared Sub FindTextBoxNamesChild(ByVal ctrl As Control, ByVal txt As ArrayList)
                For Each item As Control In ctrl.Controls
    
                    If item.[GetType]().ToString() = "System.Web.UI.WebControls.TextBox" Then
                        Dim txb As TextBox = CType(item, TextBox)
                        txt.Add(txb.ClientID.ToString)
                    End If
    
                    FindTextBoxNamesChild(item, txt)
                Next
            End Sub

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 19, 2019 7:14 PM