Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

TreeNode.SelectAction Property

Gets or sets the event or events to raise when a node is selected.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)
public TreeNodeSelectAction SelectAction { get; set; }

Property Value

Type: System.Web.UI.WebControls.TreeNodeSelectAction
One of the TreeNodeSelectAction values. The default is TreeNodeSelectAction.Select.

The text of a node in the TreeView control can be in one of two modes: selection mode or navigation mode. By default, a node is in selection mode. To put a node into navigation mode, set the node's NavigateUrl property to a value other than an empty string (""). To put a node into selection mode, set the node's NavigateUrl property to an empty string.

NoteNote

When a node is in navigation mode, selection events are disabled for that node. Clicking a node will direct the user to the specified URL, rather than posting the page back to the server and raising an event.

When a node is in selection mode, use the SelectAction property to specify which event or events are raised when a node is selected. The following table lists the available options.

Selection action

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 the TreeNodeExpanded events when a node is selected. Nodes are only expanded, never collapsed.

NoteNote

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

The value of this property is stored in view state.

TopicLocation
How to: Add or Delete TreeView Node Elements (Visual Studio)Building ASP .NET Web Applications in Visual Studio

The following code example demonstrates how to use the SelectAction property to specify which event is raised when a node is clicked.



<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  void PopulateNode(Object sender, TreeNodeEventArgs e)
  {

    // Call the appropriate method to populate a node at a particular level.
    switch(e.Node.Depth)
    {
      case 0:
        // Populate the first-level nodes.
        PopulateCategories(e.Node);
        break;
      case 1:
        // Populate the second-level nodes.
        PopulateProducts(e.Node);
        break;
      default:
        // Do nothing.
        break;
    }

  }

  void PopulateCategories(TreeNode node)
  {

    // Query for the product categories. These are the values
    // for the second-level nodes.
    DataSet ResultSet = RunQuery("Select CategoryID, CategoryName From Categories");

    // Create the second-level nodes.
    if(ResultSet.Tables.Count > 0)
    {

      // Iterate through and create a new node for each row in the query results.
      // Notice that the query results are stored in the table of the DataSet.
      foreach (DataRow row in ResultSet.Tables[0].Rows)
      {

        // Create the new node. Notice that the CategoryId is stored in the Value property 
        // of the node. This will make querying for items in a specific category easier when
        // the third-level nodes are created. 
        TreeNode newNode = new TreeNode();
        newNode.Text = row["CategoryName"].ToString(); 
        newNode.Value = row["CategoryID"].ToString();        

        // Set the PopulateOnDemand property to true so that the child nodes can be 
        // dynamically populated.
        newNode.PopulateOnDemand = true;

        // Set additional properties for the node.
        newNode.SelectAction = TreeNodeSelectAction.Expand;

        // Add the new node to the ChildNodes collection of the parent node.
        node.ChildNodes.Add(newNode);

      }

    }

  }

  void PopulateProducts(TreeNode node)
  {

    // Query for the products of the current category. These are the values
    // for the third-level nodes.
    DataSet ResultSet = RunQuery("Select ProductName From Products Where CategoryID=" + node.Value);

    // Create the third-level nodes.
    if(ResultSet.Tables.Count > 0)
    {

      // Iterate through and create a new node for each row in the query results.
      // Notice that the query results are stored in the table of the DataSet.
      foreach (DataRow row in ResultSet.Tables[0].Rows)
      {

        // Create the new node.
        TreeNode NewNode = new TreeNode(row["ProductName"].ToString());

        // Set the PopulateOnDemand property to false, because these are leaf nodes and
        // do not need to be populated.
        NewNode.PopulateOnDemand = false;

        // Set additional properties for the node.
        NewNode.SelectAction = TreeNodeSelectAction.None;

        // Add the new node to the ChildNodes collection of the parent node.
        node.ChildNodes.Add(NewNode);

      }

    }

  }

  DataSet RunQuery(String QueryString)
  {

    // Declare the connection string. This example uses Microsoft SQL Server 
    // and connects to the Northwind sample database.
    String ConnectionString = "server=localhost;database=NorthWind;Integrated Security=SSPI"; 

    SqlConnection DBConnection = new SqlConnection(ConnectionString);
    SqlDataAdapter DBAdapter;
    DataSet ResultsDataSet = new DataSet();

    try
    {

      // Run the query and create a DataSet.
      DBAdapter = new SqlDataAdapter(QueryString, DBConnection);
      DBAdapter.Fill(ResultsDataSet);

      // Close the database connection.
      DBConnection.Close();

    }
    catch(Exception ex)
    {

      // Close the database connection if it is still open.
      if(DBConnection.State == ConnectionState.Open)
      {
        DBConnection.Close();
      }

      Message.Text = "Unable to connect to the database.";

    }

    return ResultsDataSet;

  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>TreeView PopulateNodesFromClient Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>TreeView PopulateNodesFromClient Example</h3>

      <asp:TreeView id="LinksTreeView"
        Font-Names= "Arial"
        ForeColor="Blue"
        EnableClientScript="true"
        PopulateNodesFromClient="true"  
        OnTreeNodePopulate="PopulateNode"
        runat="server">

        <Nodes>

          <asp:TreeNode Text="Inventory" 
            SelectAction="Expand"  
            PopulateOnDemand="true"/>

        </Nodes>

      </asp:TreeView>

      <br /><br />

      <asp:Label id="Message" runat="server"/>

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



.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.