Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Looping through all the controls
Message
De
20/01/2009 18:51:02
 
 
À
20/01/2009 18:44:08
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Divers
Thread ID:
01375750
Message ID:
01375752
Vues:
7
>I am using a Custom Control (based on a Panel) to contain all sorts of controls, which in turn contain all sorts of controls, which may contain all sorts of controls.
>
>I need to iterate through all the controls so that I can set the BindingSource at runtime. Does anyone know an easy way to get to all of the controls, a simple foreach at the top level won't cut it.

Here is an example:
        ' Loop through all the controls on the page and apply the business logic
        ' expO1 Control
        Public Function PageControlRecursiveLoop(ByVal toControl As Object) As Boolean
            Dim lcKey As String = ""
            Dim lcID As String = ""
            Dim lcMessage As String = ""
            Dim loControl As System.Web.UI.Control
            Dim loControlButton As System.Web.UI.WebControls.Button
            Dim loControlForm As System.Web.UI.HtmlControls.HtmlForm
            Dim loControlImage As System.Web.UI.WebControls.Image
            Dim loControlImageMap As System.Web.UI.WebControls.ImageMap
            Dim loControlLabel As System.Web.UI.WebControls.Label

            ' Label values
            For Each loControl In toControl.Controls

                ' Label
                If TypeOf loControl Is System.Web.UI.WebControls.Label Then
                    loControlLabel = CType(loControl, System.Web.UI.WebControls.Label)
                    lcID = loControlLabel.ID
                End If

                ' Button
                If TypeOf loControl Is System.Web.UI.WebControls.Button Then
                    loControlButton = CType(loControl, System.Web.UI.WebControls.Button)
                    lcID = loControlButton.ID
                End If

                ' Image
                If loControl.GetType().ToString = "System.Web.UI.WebControls.Image" Then
                    loControlImage = CType(loControl, System.Web.UI.WebControls.Image)
                End If

                ' Image MAP
                If loControl.GetType().ToString = "System.Web.UI.WebControls.ImageMap" Then
                    loControlImageMap = CType(loControl, System.Web.UI.WebControls.ImageMap)
                End If

                ' Form
                If loControl.GetType().ToString = "System.Web.UI.HtmlControls" Then
                    loControlForm = CType(loControl, System.Web.UI.HtmlControls.HtmlForm)
                End If

                ' If we have child controls
                If loControl.HasControls Then
                    PageControlRecursiveLoop(loControl, tcAlias)
                End If

            Next
            Return True
        End Function
I removed some logic in each of the control type. But, this should give you a good idea on how to process them.
Michel Fournier
Level Extreme Inc.
Designer, architect, owner of the Level Extreme Platform
Subscribe to the site at https://www.levelextreme.com/Home/DataEntry?Activator=55&NoStore=303
Subscription benefits https://www.levelextreme.com/Home/ViewPage?Activator=7&ID=52
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform