This documentation is archived and is not being maintained.

TreeView Web Server Control Events

The TreeView Web server control provides several events that you can program. This allows you to run your custom routines whenever an event occurs.

TreeView control events are raised only when a user interacts with the control by doing such things as selecting, expanding, or collapsing a node. They are not raised if the select, expand, or collapse methods are called programmatically. For example, if you call the Expand method, no event will be raised.

The following table describes the events that are supported by the TreeView control.




Occurs when a check box of the TreeView control changes state between posts to the server. Occurs once for every TreeNode object that changes.


Occurs when a node is selected in the TreeView control.


Occurs when a node is expanded in the TreeView control.


Occurs when a node is collapsed in the TreeView control.


Occurs when a node, with its PopulateOnDemand property set to true, is expanded in the TreeView control.


Occurs when a data item is bound to a node in the TreeView control.

The SelectedNodeChanged Event

The following code example demonstrates handling the SelectedNodeChanged event and accessing the SelectedNode property that raised the event. It sets the text of MyLabel to be the ToolTip property text of the SelectedNode.

protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    MyLabel.Text = TreeView1.SelectedNode.ToolTip;

The TreeNodeExpanded and TreeNodeCollapsed Events

The following code example demonstrates handling the TreeNodeCollapsed event and the TreeNodeExpanded event and then accessing the TreeNode object that was collapsed or expanded.

protected void TreeView1_TreeNodeCollapsed(object sender, TreeNodeEventArgs e)
    MyLabel.Text = "You collapsed the " + e.Node.Value + " node.";
protected void TreeView1_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
    MyLabel.Text = "You expanded the " + e.Node.Value + " node.";

The TreeNodePopulate Event

The following code example demonstrates handling the TreeNodePopulate event and then programmatically adding a new TreeNode object to the ChildNodes collection of the node that raised the event.

protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
    e.Node.ChildNodes.Add(new TreeNode("New Node Populated on Demand"));