Export (0) Print
Expand All

TreeNode.Depth Property

Note: This property is new in the .NET Framework version 2.0.

Gets the depth of the node.

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in system.web.dll)

public int Depth { get; }
/** @property */
public int get_Depth ()

public function get Depth () : int

Property Value

The depth of the node.

Use the Depth property to determine the depth of the node. The depth represents the number of levels of hierarchy between a node and the root node. For example, a root node has a depth of zero. A child of the root node has a depth of one, and so on.

The following code example demonstrates how use the Depth property to determine the depth of a node. It initializes all nodes with a depth of one to a selected state. For this example to work correctly, you must copy the sample XML data below to a file named Newsgroup.xml.


<%@ Page Language="C#" %>

<script runat="server">

  void Data_Bound(Object sender, TreeNodeEventArgs e)
  {

    // Check the depth of a node as it is being bound to data.
    // Initialize the Checked property to true if the depth is 1.
    if(e.Node.Depth == 1)
    {

      e.Node.Checked = true;

    }
    else
    {

      e.Node.Checked = false;

    }

  }

</script>

<html>
  <body>
    <form runat="server">
    
      <h3>TreeNode Checked Example</h3>
    
      <asp:TreeView id="NewsgroupTreeView" 
        DataSourceID="NewsgroupXmlDataSource"
        OnTreeNodeDataBound="Data_Bound"
        ShowCheckBoxes="All"
        InitialExpandDepth="2"  
        runat="server">
         
        <DataBindings>
          <asp:TreeNodeBinding DataMember="category" TextField="Name"/>
          <asp:TreeNodeBinding DataMember="group" TextField="Name"/>
        </DataBindings>
         
      </asp:TreeView>
      
      <asp:XmlDataSource id="NewsgroupXmlDataSource"  
        DataFile="Newsgroup.xml"
        runat="server">
      </asp:XmlDataSource>
    
    </form>
  </body>
</html>


The following code is sample XML data for the previous example.

<category name="news.microsoft.com">
    <group name="microsoft.public.dotnet.framework.aspnet"/>
    <group name="microsoft.public.dotnet.framework.aspnet.mobile"/>
    <group name="microsoft.public.dotnet.framework.aspnet.webservices"/>
</category>

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2015 Microsoft