Export (0) Print
Expand All
0 out of 3 rated this helpful - Rate this topic

TreeView.ShowCheckBoxes Property

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

Gets or sets a value indicating which node types will display a check box in the TreeView control.

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

public TreeNodeTypes ShowCheckBoxes { get; set; }
/** @property */
public TreeNodeTypes get_ShowCheckBoxes ()

/** @property */
public void set_ShowCheckBoxes (TreeNodeTypes value)

public function get ShowCheckBoxes () : TreeNodeTypes

public function set ShowCheckBoxes (value : TreeNodeTypes)

Property Value

A bitwise combination of the TreeNodeTypes values. The default is TreeNodeType.None.
Exception typeCondition

ArgumentOutOfRangeException

The bitwise combination value is outside the range of the TreeNodeTypes enumeration.

To provide multi-node selection support in the TreeView control, you can display check boxes next to the image for a node. Use the ShowCheckBoxes property to specify which node types will display a check box. For example, if this property is set to TreeNodeType.Parent, check boxes are displayed for each parent node in the tree. The following table lists the valid values for this property.

Node type

Description

TreeNodeType.All

Check boxes are displayed for all nodes.

TreeNodeType.Leaf

Check boxes are displayed for all leaf nodes.

TreeNodeType.None

Check boxes are not displayed.

TreeNodeType.Parent

Check boxes are displayed for all parent nodes.

TreeNodeType.Root

Check boxes are displayed for all root nodes.

The enumeration type that is used for the ShowCheckBoxes property is a flag enumeration, which allows you to combine values through bitwise operations. For example, to display check boxes for the parent and leaf nodes, use the bitwise OR operator to combine the TreeNodeType.Parent and TreeNodeType.Leaf values.

To determine which nodes have their check box selected, iterate through the nodes of the CheckedNodes collection.

NoteNote

You can override the ShowCheckBoxes setting by setting the ShowCheckBox property for each node.

If you need to provide only single-selection support, consider using the SelectedNode property.

The value of this property is stored in view state.

The following code example demonstrates how to use the ShowCheckBoxes property to show and hide the check boxes for the different node types.


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

<script runat="server">

  void Button_Click(Object sender, EventArgs e)
  {

    if(LinksTreeView.CheckedNodes.Count > 0)
    {

      // Clear the message label.
      Message.Text = "You selected: <br><br>";

      // Iterate through the CheckedNodes collection and display the selected nodes.
      foreach (TreeNode node in LinksTreeView.CheckedNodes)
      {

        Message.Text += node.Text + "<br>";

      }

    }

    else
    {

      Message.Text = "No items selected.";

    }

  }

</script>

<html>
  <body>
    <form runat="server">
    
      <h3>TreeView ShowCheckBoxes Example</h3>
    
      <!-- Set the ShowCheckBoxes property declaratively.   -->
      <!-- Because the ShowCheckBoxes property uses a flag  -->
      <!-- enumeration, you can combine multiple values by  -->
      <!-- using the bitwise OR operator. In declarative    -->
      <!-- syntax, this is done using a comma separated     -->
      <!-- list.                                            -->
      <asp:TreeView id="LinksTreeView"
        Font-Name= "Arial"
        ForeColor="Blue"
        InitialExpandDepth="2"
        ShowCheckBoxes="Parent,Leaf" 
        runat="server">
         
        <LevelStyles>
        
          <asp:TreeNodeStyle ChildNodesPadding="10" 
            Font-Bold="true" 
            Font-Size="12pt" 
            ForeColor="DarkGreen"/>
          <asp:TreeNodeStyle ChildNodesPadding="5" 
            Font-Bold="true" 
            Font-Size="10pt"/>
          <asp:TreeNodeStyle ChildNodesPadding="5" 
            Font-UnderLine="true" 
            Font-Size="10pt"/>
          <asp:TreeNodeStyle ChildNodesPadding="10" 
            Font-Size="8pt"/>
             
        </LevelStyles>
         
        <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 Text="Chapter Two">
            
              <asp:TreeNode Text="Section 2.0">
              
                <asp:TreeNode Text="Topic 2.0.1"/>
                <asp:TreeNode Text="Topic 2.0.2"/>
              
              </asp:TreeNode>
            
            </asp:TreeNode>
            
          </asp:TreeNode>
          <asp:TreeNode Text="Appendix A" />
          <asp:TreeNode Text="Appendix B" />
          <asp:TreeNode Text="Appendix C" />
        
        </Nodes>
        
      </asp:TreeView>
      
      <br><br>
      
      <asp:Button id="Submit"
        Text="Select Items"
        OnClick="Button_Click"  
        runat="server"/>
         
      <br><br>
      
      <asp:Label id="Message"
        runat="server"/>

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


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
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.