Export (0) Print
Expand All

TreeNode.Depth Property

Gets the depth of the node.

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

'Declaration
<BrowsableAttribute(False)> _
Public ReadOnly Property Depth As Integer

Property Value

Type: System.Int32
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="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  Sub Data_Bound(ByVal sender As Object, ByVal e As TreeNodeEventArgs)

    ' 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 Then

      e.Node.Checked = True

    Else

      e.Node.Checked = False

    End If

  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>TreeNode Checked Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>TreeNode Checked Example</h3>

      <asp:TreeView id="NewsgroupTreeView" 
        DataSourceID="NewsgroupXmlDataSource"
        OnTreeNodeDataBound="Data_Bound"
        ShowCheckBoxes="All"
        ExpandDepth="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>

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0
Show:
© 2015 Microsoft