共用方式為


Selection, Navigation, and Check Boxes in the TreeView Web Server Control

The ASP.NET TreeView control makes it easy for you to respond when a user clicks a node, or to change the behavior of a node to make it act like a hyperlink. Additionally, you can use the TreeView control to display check boxes next to individual nodes so that users can select multiple nodes at once.

Selection and Navigation

Each node has a Text property and a Value property. The value of the Text property is displayed in the TreeView control, while the Value property is used to store any additional data about the node, such as data passed to the postback event that is associated with the node.

When a node of the TreeView control is clicked, it can either raise a selection event (via postback) or go to another page. When the NavigateUrl property is not set, clicking a node will raise a SelectedNodeChanged event that can be handled to provide custom functionality. You can also check the Selected property of a node to determine whether it is selected. Setting this property to true or calling the Select method on the node will cause the node to be selected programmatically.

Each node also has a SelectAction property that can be used to specify actions that will occur when the node is clicked, such as expanding or collapsing the node. Instead of raising a SelectedNodeChanged event to go to another page when a node is clicked, set the NavigateUrl property of the node to a value other than an empty string ("").

For more information about handling selection events, see SelectedNode. For more information about navigation mode, see NavigateUrl. For more information about setting the NavigateUrl property when binding data, see NavigateUrlField.

Check Boxes

In cases where you want users to be able to select multiple nodes, you can use the TreeView control to display a check box next to a node image. When the ShowCheckBoxes property is set to a TreeNodeType other than TreeNodeType.None, check boxes are displayed next to the specified nodes. When check boxes are displayed, you can use the TreeNodeCheckChanged event to run your custom routine whenever the state of a check box changes between posts to the server. The next time the page is posted to the server, the CheckedNodes property is populated with the selected nodes.

The TreeView control does not expose an AutoPostBack property. Thus, to submit check box selections, you must place a control, such as a Button Web server control, on the page to post information back to the server.

For more information about using check boxes with the ASP.NET TreeView control, see CheckBoxes.

See Also

Reference

TreeView Web Server Control Overview

Concepts

Populating Tree Nodes in the TreeView Web Server Control