TreeNode.Checked 属性

定义

获取或设置一个值,该值指示节点的复选框是否被选中。

public:
 property bool Checked { bool get(); void set(bool value); };
public bool Checked { get; set; }
member this.Checked : bool with get, set
Public Property Checked As Boolean

属性值

如果节点的复选框被选中,则为 true;否则为 false。 默认值为 false

示例

下面的代码示例演示如何使用 Checked 属性指定是否选中节点的复选框。 它将深度为 1 的节点的复选框初始化为所选状态。 若要使此示例正常工作,必须将以下示例 XML 数据复制到名为 Newsgroup.xml 的文件。


<%@ 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 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 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>

<%@ 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>

以下代码是上一示例的示例 XML 数据。

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

注解

当节点显示复选框时, Checked 通常使用 属性来指定是否选中该复选框。 选中与节点关联的复选框后,该节点将自动添加到 CheckedNodes 控件的集合中 TreeView 。 属性 Checked 还可用于确定是否选中复选框。

注意

更常见的情况是通过循环CheckedNodes访问集合来确定控件中TreeView哪些节点的复选框处于选中状态。

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

适用于

另请参阅