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.
Events
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.
Event |
Description |
---|---|
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. |
Example
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 Sub TreeView1_SelectedNodeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.SelectedNodeChanged
MyLabel.Text = TreeView1.SelectedNode.ToolTip
End Sub
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 Sub TreeView1_TreeNodeCollapsed(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles TreeView1.TreeNodeCollapsed
MyLabel.Text = "You collapsed the " & e.Node.Value & " node."
End Sub
Protected Sub TreeView1_TreeNodeExpanded(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles TreeView1.TreeNodeExpanded
MyLabel.Text = "You expanded the " & e.Node.Value & " node."
End Sub
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 Sub TreeView1_TreeNodePopulate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles TreeView1.TreeNodePopulate
e.Node.ChildNodes.Add(New TreeNode("New Node Populated on Demand"))
End Sub
protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
e.Node.ChildNodes.Add(new TreeNode("New Node Populated on Demand"));
}
See Also
Reference
TreeView Web Server Control Overview
Concepts
Populating Tree Nodes in the TreeView Web Server Control
Customizing the Look and Feel of the TreeView Web Server Control
Selection, Navigation, and Check Boxes in the TreeView Web Server Control