Export (0) Print
Expand All

IHierarchyData.GetParent Method

Gets an IHierarchyData object that represents the parent node of the current hierarchical node.

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)

IHierarchyData GetParent()

Return Value

Type: System.Web.UI.IHierarchyData
An IHierarchyData object that represents the parent node of the current hierarchical node.

The IHierarchyData interface does not define a HasParent convenience property, so callers must check the return value of the GetParent method for null to determine whether the current IHierarchyData node is the root node of the hierarchical data structure to which the node belongs.

The following code example demonstrates how to implement the GetParent method in a class that implements the IHierarchyData interface. The FileSystemHierarchyData class wraps a FileSystemInfo object, and the GetParent method implementation checks the type of the FileSystemInfo object, and returns the appropriate parent object based on the type. This code example is part of a larger example provided for the IHierarchyData interface and the HierarchicalDataSourceControl class.

public IHierarchicalEnumerable GetChildren()
{
    FileSystemHierarchicalEnumerable children =
        new FileSystemHierarchicalEnumerable();

    if (typeof(DirectoryInfo) == fileSystemObject.GetType())
    {
        DirectoryInfo temp = (DirectoryInfo)fileSystemObject;
        foreach (FileSystemInfo fsi in temp.GetFileSystemInfos())
        {
            children.Add(new FileSystemHierarchyData(fsi));
        }
    }
    return children;
}

public IHierarchyData GetParent()
{
    FileSystemHierarchicalEnumerable parentContainer =
        new FileSystemHierarchicalEnumerable();

    if (typeof(DirectoryInfo) == fileSystemObject.GetType())
    {
        DirectoryInfo temp = (DirectoryInfo)fileSystemObject;
        return new FileSystemHierarchyData(temp.Parent);
    }
    else if (typeof(FileInfo) == fileSystemObject.GetType())
    {
        FileInfo temp = (FileInfo)fileSystemObject;
        return new FileSystemHierarchyData(temp.Directory);
    }
    // If FileSystemObj is any other kind of FileSystemInfo, ignore it. 
    return null;
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft