TreeNodeBinding.SelectAction Property

Definition

Gets or sets the event or events to raise when a node to which the TreeNodeBinding object is applied is selected.

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

Property Value

One of the TreeNodeSelectAction values. The default is TreeNodeSelectAction.Select.

Examples

This section contains two code examples. The first code example demonstrates how to use the SelectAction property to specify which event is raised when a node is clicked. The second code example provides sample XML data for the first code example.

The following example demonstrates how to use the SelectAction property to specify which event is raised when a node is clicked. For this example to work correctly, you must copy the sample XML data, provided after this code example, to a file named Book.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 Node_Changed(Object sender, EventArgs e)
  {

    Message.Text = BookTreeView.SelectedNode.Text + " node selected.";

  }

  void Node_Expanded(Object sender, TreeNodeEventArgs e)
  {

    Message.Text = e.Node.Text + " node expanded.";

  }

  void Node_Collapsed(Object sender, TreeNodeEventArgs e)
  {

    Message.Text = "";

  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>TreeNodeBinding SelectAction Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>TreeNodeBinding SelectAction Example</h3>
    
      <asp:TreeView id="BookTreeView"
        Font-Names= "Arial"
        ForeColor="Blue"
        DataSourceID="BookXmlDataSource"
        EnableClientScript="false" 
        OnSelectedNodeChanged="Node_Changed"
        OnTreeNodeExpanded="Node_Expanded"
        OnTreeNodeCollapsed="Node_Collapsed"   
        runat="server">
        
        <DataBindings>
          <asp:TreeNodeBinding DataMember="Book" 
            TextField="Title"
            SelectAction="Expand"/>
          <asp:TreeNodeBinding DataMember="Chapter" 
            TextField="Heading"
            SelectAction="Expand"/>
        </DataBindings>
        
      </asp:TreeView>
      
      <asp:XmlDataSource id="BookXmlDataSource"  
        DataFile="Book.xml"
        runat="server">
      </asp:XmlDataSource>
      
      <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 Node_Changed(ByVal sender As Object, ByVal e As EventArgs)

    Message.Text = BookTreeView.SelectedNode.Text & " node selected."

  End Sub

  Sub Node_Expanded(ByVal sender As Object, ByVal e As TreeNodeEventArgs)

    Message.Text = e.Node.Text & " node expanded."

  End Sub

  Sub Node_Collapsed(ByVal sender As Object, ByVal e As TreeNodeEventArgs)

    Message.Text = ""

  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>TreeNodeBinding SelectAction Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>TreeNodeBinding SelectAction Example</h3>
    
      <asp:TreeView id="BookTreeView"
        Font-Names= "Arial"
        ForeColor="Blue"
        DataSourceID="BookXmlDataSource"
        EnableClientScript="false" 
        OnSelectedNodeChanged="Node_Changed"
        OnTreeNodeExpanded="Node_Expanded"
        OnTreeNodeCollapsed="Node_Collapsed"   
        runat="server">
        
        <DataBindings>
          <asp:TreeNodeBinding DataMember="Book" 
            TextField="Title"
            SelectAction="Expand"/>
          <asp:TreeNodeBinding DataMember="Chapter" 
            TextField="Heading"
            SelectAction="Expand"/>
        </DataBindings>
        
      </asp:TreeView>
      
      <asp:XmlDataSource id="BookXmlDataSource"  
        DataFile="Book.xml"
        runat="server">
      </asp:XmlDataSource>
      
      <br /><br />
      
      <asp:Label id="Message" runat="server"/>
      
    </form>
  </body>
</html>

The following code example provides sample XML data for the preceding code example.

<Book Title="Book Title">  
    <Chapter Heading="Chapter 1">  
        <Section Heading="Section 1">  
        </Section>  
        <Section Heading="Section 2">  
        </Section>  
    </Chapter>  
    <Chapter Heading="Chapter 2">  
        <Section Heading="Section 1">  
        </Section>  
    </Chapter>  
    <Appendix Heading="Appendix A">  
    </Appendix>  
</Book>  

Remarks

When the TreeView control is bound to a data source, use the SelectAction property to specify the value to bind to the SelectAction property of a TreeNode object. This binding relationship affects all TreeNode objects to which the TreeNodeBinding object is applied. The SelectAction property is used to specify which event or events are raised when a node is selected.

Note

You can selectively override the SelectAction property by setting the SelectAction property of each node directly.

The following table lists the available options.

SelectAction value Description
TreeNodeSelectAction.Expand Toggles the node between expanded and collapsed. Raises the TreeNodeExpanded event or the TreeNodeCollapsed event, as appropriate.
TreeNodeSelectAction.None Raises no events when a node is selected.
TreeNodeSelectAction.Select Raises the SelectedNodeChanged event when a node is selected.
TreeNodeSelectAction.SelectExpand Raises both the SelectedNodeChanged and TreeNodeExpanded events when a node is selected. Nodes are only expanded, never collapsed.

Note

The HoverNodeStyle is not rendered for a node with its SelectAction property set to TreeNodeSelectAction.None.

The value of this property is stored in view state.

Applies to

See also