IHierarchicalEnumerable.GetHierarchyData(Object) Méthode

Définition

Retourne un élément de données hiérarchiques pour l’élément énuméré spécifié.

public:
 System::Web::UI::IHierarchyData ^ GetHierarchyData(System::Object ^ enumeratedItem);
public System.Web.UI.IHierarchyData GetHierarchyData (object enumeratedItem);
abstract member GetHierarchyData : obj -> System.Web.UI.IHierarchyData
Public Function GetHierarchyData (enumeratedItem As Object) As IHierarchyData

Paramètres

enumeratedItem
Object

Object pour lequel IHierarchyData doit être retourné.

Retours

IHierarchyData

Instance IHierarchyData qui représente le Object passé à la méthode GetHierarchyData(Object).

Exemples

L’exemple de code suivant montre comment un ASP.NET contrôle hiérarchique lié aux données utilise un IHierarchyData objet dans une méthode de liaison de données récursive. Les éléments d’un IHierarchicalEnumerable objet sont énumérés et pour chaque IHierarchyData objet est récupéré à l’aide de la GetHierarchyData méthode. Enfin, la propriété est vérifiée pour déterminer si la HasChildren récursivité est nécessaire. Cet exemple de code fait partie d’un exemple plus grand fourni pour la HierarchicalDataBoundControl classe.

private void RecurseDataBindInternal(TreeNode node, 
    IHierarchicalEnumerable enumerable, int depth) {                                    
                
    foreach(object item in enumerable) {
        IHierarchyData data = enumerable.GetHierarchyData(item);

        if (null != data) {
            // Create an object that represents the bound data
            // to the control.
            TreeNode newNode = new TreeNode();
            RootViewNode rvnode = new RootViewNode();
            
            rvnode.Node = newNode;
            rvnode.Depth = depth;

            // The dataItem is not just a string, but potentially
            // an XML node or some other container. 
            // If DataTextField is set, use it to determine which 
            // field to render. Otherwise, use the first field.                    
            if (DataTextField.Length > 0) {
                newNode.Text = DataBinder.GetPropertyValue
                    (data, DataTextField, null);
            }
            else {
                PropertyDescriptorCollection props = 
                    TypeDescriptor.GetProperties(data);

                // Set the "default" value of the node.
                newNode.Text = String.Empty;                        

                // Set the true data-bound value of the TextBox,
                // if possible.
                if (props.Count >= 1) {                        
                    if (null != props[0].GetValue(data)) {
                        newNode.Text = 
                            props[0].GetValue(data).ToString();
                    } 
                }
            }

            Nodes.Add(rvnode);                    
            
            if (data.HasChildren) {
                IHierarchicalEnumerable newEnumerable = 
                    data.GetChildren();
                if (newEnumerable != null) {                            
                    RecurseDataBindInternal(newNode, 
                        newEnumerable, depth+1 );
                }
            }
            
            if ( _maxDepth < depth) _maxDepth = depth;
        }
    }
}
Private Sub RecurseDataBindInternal(ByVal node As TreeNode, _
    ByVal enumerable As IHierarchicalEnumerable, _
    ByVal depth As Integer)

    Dim item As Object
    For Each item In enumerable

        Dim data As IHierarchyData = enumerable.GetHierarchyData(item)

        If Not data Is Nothing Then

            ' Create an object that represents the bound data
            ' to the control.
            Dim newNode As New TreeNode()
            Dim rvnode As New RootViewNode()

            rvnode.Node = newNode
            rvnode.Depth = depth

            ' The dataItem is not just a string, but potentially
            ' an XML node or some other container. 
            ' If DataTextField is set, use it to determine which 
            ' field to render. Otherwise, use the first field.                    
            If DataTextField.Length > 0 Then
                newNode.Text = DataBinder.GetPropertyValue _
                (data, DataTextField, Nothing)
            Else
                Dim props As PropertyDescriptorCollection = _
                TypeDescriptor.GetProperties(data)

                ' Set the "default" value of the node.
                newNode.Text = String.Empty

                ' Set the true data-bound value of the TextBox,
                ' if possible.
                If props.Count >= 1 Then
                    If Not props(0).GetValue(data) Is Nothing Then
                        newNode.Text = props(0).GetValue(data).ToString()
                    End If
                End If
            End If

            Nodes.Add(rvnode)

            If data.HasChildren Then
                Dim newEnumerable As IHierarchicalEnumerable = _
                    data.GetChildren()
                If Not (newEnumerable Is Nothing) Then
                    RecurseDataBindInternal(newNode, _
                    newEnumerable, depth + 1)
                End If
            End If

            If MaxDepth < depth Then
                MaxDepth = depth
            End If
        End If
    Next item

End Sub

Remarques

En règle générale, les clients qui utilisent IHierarchicalEnumerable des collections récupèrent un IEnumerator objet en appelant la GetEnumerator méthode, puis effectuent une itération dans l’énumération et appellent la GetHierarchyData méthode sur chaque élément énuméré pour récupérer un IHierarchyData objet.

S’applique à

Voir aussi