The TreeView Web Server Control provides several events that can be programmed. This allows you to run custom routines when an event occurs.
I. Events
A TreeView control event is triggered only when the user interacts with the control through some operations (such as selecting, expanding, or folding nodes. If you call the selection, expansion, or collapse method programmatically, these events are not triggered. For example, if you call the Expand method, no event is triggered.
The following table describes the events supported by the TreeView control.
Event
Description
TreeNodeCheckChanged
This occurs when the check box of the TreeView control is sent to the server for status change. Each TreeNode object changes once.
SelectedNodeChanged
This occurs when a node is selected in the TreeView control.
TreeNodeExpanded
It occurs when a node is expanded in the TreeView control.
TreeNodeCollapsed
A node is collapsed in the TreeView control.
TreeNodePopulate
This operation occurs when a PopulateOnDemand attribute is set to true in the TreeView control.
TreeNodeDataBound
It occurs when a data item is bound to a node in the TreeView control.
Ii. Example
2.1 SelectedNodeChanged event
The following code example demonstrates how to handle the SelectedNodeChanged event and how to access the SelectedNode attribute that raises the event. In this example, set the text of MyLabel To The ToolTip attribute text of SelectedNode.
PRotected void TreeView1_SelectedNodeChanged (object sender, EventArgs e)
{
MyLabel. Text = TreeView1.SelectedNode. ToolTip;
}
2.2. TreeNodeExpanded event and TreeNodeCollapsed event
The following code example demonstrates how to handle the TreeNodeCollapsed event and TreeNodeExpanded event, and how to access the collapsed or expanded TreeNode object.
Protected void treeviewinclutreenodecollapsed (object sender, TreeNodeEventArgs e)
{
MyLabel. Text = "You collapsed the" + e. Node. Value + "node .";
}
Protected void treeviewinclutreenodeexpanded (object sender, TreeNodeEventArgs e)
{
MyLabel. Text = "You expanded the" + e. Node. Value + "node .";
}
2.3 TreeNodePopulate event
The following code example demonstrates how to handle the TreeNodePopulate event and how to programmatically Add a new TreeNode object to the ChildNodes collection of nodes that raise the event.
Protected void treeviewinclutreenodepopulate (object sender, TreeNodeEventArgs e)
{
E. Node. ChildNodes. Add (new TreeNode ("New Node Populated on Demand "));