Bewerken

Share via


TreeNode.Checked Property

Definition

Gets or sets a value that indicates whether the node's check box is selected.

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

Property Value

true if the node's check box is selected; otherwise, false. The default is false.

Examples

The following code example demonstrates how to use the Checked property to specify whether a node's check box is selected. It initializes the check boxes of 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="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>

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>  

Remarks

When a node displays a check box, the Checked property is commonly used to specify whether the check box is selected. When the check box associated with a node is selected, the node is automatically added to the CheckedNodes collection of the TreeView control. The Checked property can also be used to determine whether the check box is selected.

Note

It is more common to determine which nodes in the TreeView control have their check boxes selected by iterating through the CheckedNodes collection.

The value of this property is stored in view state.

Applies to

See also