This documentation is archived and is not being maintained.

TreeNodeStyle.NodeSpacing Property

Gets or sets the amount of vertical spacing between the node to which the TreeNodeStyle object is applied and its adjacent nodes.

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

public Unit NodeSpacing { get; set; }

Property Value

Type: System.Web.UI.WebControls.Unit
The amount of vertical space, in pixels, between the node to which the TreeNodeStyle is applied and its adjacent nodes at the same level. The default is 0 pixels.

ExceptionCondition
ArgumentOutOfRangeException

The selected value is of type Percentage or is less than 0.

Use the NodeSpacing property to control the amount of vertical spacing between a node that the TreeNodeStyle is applied to and its adjacent nodes at the same level. This space is inserted between the upper edge of the current node and the lower edge of the previous node, as well as between the lower edge of the current node and upper edge of the next node. The upper space is not rendered for the first node in the TreeView control, because this would leave an unnecessary gap at the top of the control.

The NodeSpacing and ChildNodesPadding properties are applied independently. Whether or not the node is expanded does not affect the node spacing.

The value of this property is stored in view state.

The following code example demonstrates how to use the NodeSpacing property to specify the vertical spacing between a node and its adjacent nodes at the same level in the tree.

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

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

  void HorizontalPadding_Changed(Object sender, EventArgs e)
  {

    // Programmatically set the HorizontalPadding property based on the  
    // user's selection.
    ItemsTreeView.ParentNodeStyle.HorizontalPadding = Convert.ToInt32(HorizontalPaddingList.SelectedItem.Text);

  }

  void VerticalPadding_Changed(Object sender, EventArgs e)
  {

    // Programmatically set the VerticalPadding property based on the  
    // user's selection.
    ItemsTreeView.ParentNodeStyle.VerticalPadding = Convert.ToInt32(VerticalPaddingList.SelectedItem.Text);

  }

  void NodeSpacing_Changed(Object sender, EventArgs e)
  {

    // Programmatically set the NodeSpacing property based on the  
    // user's selection.
    ItemsTreeView.ParentNodeStyle.NodeSpacing = Convert.ToInt32(NodeSpacingList.SelectedItem.Text);

  }

  void ChildNodePadding_Changed(Object sender, EventArgs e)
  {

    // Programmatically set the ChildNodesPadding property based on the  
    // user's selection.
    ItemsTreeView.ParentNodeStyle.ChildNodesPadding = Convert.ToInt32(ChildNodesPaddingList.SelectedItem.Text);

  }

</script>

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

      <h3>TreeNodeStyle Example</h3>

      <!-- Set the styles for the leaf nodes declaratively. -->
      <asp:TreeView id="ItemsTreeView"
        Font-Names= "Arial"
        ForeColor="Blue"
        ParentNodeStyle-ForeColor="Green"
        ParentNodeStyle-HorizontalPadding="5" 
        ParentNodeStyle-VerticalPadding="5"  
        ParentNodeStyle-NodeSpacing="5"
        ParentNodeStyle-ChildNodesPadding="5"
        ExpandDepth="4"  
        runat="server">

        <Nodes>

          <asp:TreeNode Text="Table of Contents"
            SelectAction="None">

            <asp:TreeNode Text="Chapter One">

              <asp:TreeNode Text="Section 1.0">

                <asp:TreeNode Text="Topic 1.0.1"/>
                <asp:TreeNode Text="Topic 1.0.2"/>
                <asp:TreeNode Text="Topic 1.0.3"/>

              </asp:TreeNode>

              <asp:TreeNode Text="Section 1.1">

                <asp:TreeNode Text="Topic 1.1.1"/>
                <asp:TreeNode Text="Topic 1.1.2"/>
                <asp:TreeNode Text="Topic 1.1.3"/>
                <asp:TreeNode Text="Topic 1.1.4"/>

              </asp:TreeNode>

            </asp:TreeNode>

          </asp:TreeNode>

        </Nodes>

      </asp:TreeView>

      <hr />

      <h5>Select the style settings for the parent nodes.</h5>

      <table cellpadding="5">

        <tr align="right">

          <td>

            Horizontal Padding:

            <asp:DropDownList id="HorizontalPaddingList"
              AutoPostBack="true"
              OnSelectedIndexChanged="HorizontalPadding_Changed" 
              runat="server">

              <asp:ListItem>0</asp:ListItem>
              <asp:ListItem Selected="true">5</asp:ListItem>
              <asp:ListItem>10</asp:ListItem>

            </asp:DropDownList> 

          </td>

          <td>

            Vertical Padding:

            <asp:DropDownList id="VerticalPaddingList"
              AutoPostBack="true"
              OnSelectedIndexChanged="VerticalPadding_Changed" 
              runat="server">

              <asp:ListItem>0</asp:ListItem>
              <asp:ListItem Selected="true">5</asp:ListItem>
              <asp:ListItem>10</asp:ListItem>

            </asp:DropDownList> 

          </td>

        </tr>

        <tr align="right">

          <td>

            Node Spacing:

            <asp:DropDownList id="NodeSpacingList"
              AutoPostBack="true"
              OnSelectedIndexChanged="NodeSpacing_Changed"   
              runat="server">

              <asp:ListItem>0</asp:ListItem>
              <asp:ListItem Selected="true">5</asp:ListItem>
              <asp:ListItem>10</asp:ListItem>

            </asp:DropDownList> 

          </td>

          <td>

            Child Nodes Padding:

            <asp:DropDownList id="ChildNodesPaddingList"
              AutoPostBack="true"
              OnSelectedIndexChanged="ChildNodePadding_Changed"  
              runat="server">

              <asp:ListItem>0</asp:ListItem>
              <asp:ListItem Selected="true">5</asp:ListItem>
              <asp:ListItem>10</asp:ListItem>

            </asp:DropDownList> 

          </td>

        </tr>

      </table>

    </form>
  </body>
</html>

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0
Show: