The
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
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 |
|
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 |
|
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 MyLabel
to be the
Visual BasicВ | Copy Code |
---|---|
Protected Sub TreeView1_SelectedNodeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.SelectedNodeChanged MyLabel.Text = TreeView1.SelectedNode.ToolTip End Sub |
C#В | Copy Code |
---|---|
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.
Visual BasicВ | Copy Code |
---|---|
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 |
C#В | Copy Code |
---|---|
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
Visual BasicВ | Copy Code |
---|---|
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 |
C#В | Copy Code |
---|---|
protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e) { e.Node.ChildNodes.Add(new TreeNode("New Node Populated on Demand")); } |