TreeView.ShowCheckBoxes 属性

定义

获取或设置一个值,它指示哪些节点类型将在 TreeView 控件中显示复选框。

public:
 property System::Web::UI::WebControls::TreeNodeTypes ShowCheckBoxes { System::Web::UI::WebControls::TreeNodeTypes get(); void set(System::Web::UI::WebControls::TreeNodeTypes value); };
public System.Web.UI.WebControls.TreeNodeTypes ShowCheckBoxes { get; set; }
member this.ShowCheckBoxes : System.Web.UI.WebControls.TreeNodeTypes with get, set
Public Property ShowCheckBoxes As TreeNodeTypes

属性值

TreeNodeTypes

TreeNodeTypes 值的按位组合。 默认值为 TreeNodeType.None

例外

按位组合值在 TreeNodeTypes 枚举的范围之外。

示例

下面的代码示例演示如何使用 ShowCheckBoxes 属性显示和隐藏不同节点类型的复选框。


<%@ 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 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 xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>TreeView ShowCheckBoxes Example</title>
</head>
<body>
    <form id="form1" 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-Names= "Arial"
        ForeColor="Blue"
        ExpandDepth="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>

<%@ 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 Button_Click(ByVal sender As Object, ByVal e As EventArgs)

    If LinksTreeView.CheckedNodes.Count > 0 Then

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

      ' Iterate through the CheckedNodes collection and display the selected nodes.
      Dim node As TreeNode
      
      For Each node In LinksTreeView.CheckedNodes

        Message.Text &= node.Text & "<br />"

      Next

    Else

      Message.Text = "No items selected."

    End If

  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>TreeView ShowCheckBoxes Example</title>
</head>
<body>
    <form id="form1" 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-Names= "Arial"
        ForeColor="Blue"
        ExpandDepth="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>

注解

若要在 TreeView 控件中提供多节点选择支持,可以在节点的图像旁边显示复选框。 使用 ShowCheckBoxes 属性指定将显示复选框的节点类型。 例如,如果此属性设置为 TreeNodeType.Parent,则会为树中的每个父节点显示复选框。 下表列出了此属性的有效值。

节点类型 描述
TreeNodeType.All 所有节点都显示复选框。
TreeNodeType.Leaf 将显示所有叶节点的复选框。
TreeNodeType.None 不显示复选框。
TreeNodeType.Parent 将显示所有父节点的复选框。
TreeNodeType.Root 将显示所有根节点的复选框。

用于属性的 ShowCheckBoxes 枚举类型是一个标志枚举,它允许通过按位操作合并值。 例如,若要显示父节点和叶节点的复选框,请使用按位 OR 运算符合并 TreeNodeType.ParentTreeNodeType.Leaf 值。

若要确定哪些节点已选中其复选框,请循环访问集合的 CheckedNodes 节点。

备注

可以通过为每个节点设置ShowCheckBox属性来替代ShowCheckBoxes设置。

如果需要仅提供单选支持,请考虑使用 SelectedNode 属性。

此属性的值以视图状态存储。

适用于

另请参阅