Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to create treeview in asp.net (vb.net)

Status
Not open for further replies.

123ASP

MIS
Nov 2, 2002
239
US
Hi and thanks for your help.
I am trying to create something similar to the following link:


Since I am new to asp.net development, I am not sure how, so I appreciate your guidance and assistance.
thanks
Al
 
123: Have you been by 's control section? jfrost suggested in a earlier post that they have several varieties of T-view controls (also check Microsoft). If you don't have any luck there, I have some code snippets with treeview design.
 
hi Sadore,
I guess your code will be helpful . and thanks for your help
Al
 
Couple of Examples:

**** Simple Tree View

<%@ Import Namespace=&quot;Microsoft.Web.UI.WebControls&quot; %>
<%@ Register TagPrefix=&quot;IE&quot; Namespace=&quot;Microsoft.Web.UI.WebControls&quot; Assembly=&quot;Microsoft.Web.UI.WebControls&quot; %>

<html>
<head><title>SimpleTreeView.aspx</title></head>
<body>
<form runat=&quot;server&quot;>

<IE:TreeView
ruant=&quot;server&quot;>

<TreeNode
Text=&quot;ASP.NET Books&quot;> ' <-- open >

<TreeNode
Text=&quot;VB.NET Books&quot;/> ' <-- closed />

<TreeNode
Text=&quot;ASP.NET Tips&quot;/>

</TreeNode>
</IE:TreeView>

</form>
</body>
</html>

**** Detecting TreeView Node Selection

<%@ Import Namespace=&quot;Microsoft.Web.UI.WebControls&quot; %>
<%@ Register TagPrefix=&quot;IE&quot; Namespace=&quot;Microsoft.Web.UI.WebControls&quot; Assembly=&quot;Microsoft.Web.UI.WebControls&quot; %>

<script runat=&quot;server&quot;>

Sub TreeView_SelectedINdexChanged(s As Object, e As TreeViewSelectEventArgs)
lblSelectedNode.Text = treeBooks.GetNodeFromIndex(e.NewNode).Text
End Sub
</script>

<html>
<head><title>TreeViewSelectedIndexChanged.aspx</title></head>
<body>
<form runat=&quot;server&quot;>

<IE:TreeView
AutoPostBack=&quot;True&quot;
OnSelectedIndexChanged=&quot;TreeView_SelectedIndexChanged&quot;
ruant=&quot;server&quot;>

<TreeNode
Text=&quot;ASP.NET Books&quot;>

<TreeNode
Text=&quot;VB.NET Books&quot;/>

<TreeNode
Text=&quot;ASP.NET Tips&quot;/>

</TreeNode>
</IE:TreeView>

<p>

<asp:Label
id=&quot;lblSelectedNode&quot;
ruant=&quot;server&quot; />

</form>
</body>
</html>

**** Associating URL's with TreeView Nodes

<%@ Import Namespace=&quot;Microsoft.Web.UI.WebControls&quot; %>
<%@ Register TagPrefix=&quot;IE&quot; Namespace=&quot;Microsoft.Web.UI.WebControls&quot; Assembly=&quot;Microsoft.Web.UI.WebControls&quot; %>

<html>
<head><title>SimpleTreeView.aspx</title></head>
<body>
<form runat=&quot;server&quot;>

<IE:TreeView
ruant=&quot;server&quot;>

<TreeNode
Text=&quot;My favorite URLs&quot;>

<TreeNode
Text=&quot;ASP.NET Books&quot;
NavigateURL=&quot; />

<TreeNode
Text=&quot;VB.NET Books&quot;
NavigateURL=&quot; />

<TreeNode
Text=&quot;ASP.NET Tips&quot;
NavigateURL=&quot; />

</TreeNode>
</IE:TreeView>

</form>
</body>
</html>

When you select a new node in the tree view, the TreeView_SelectedIndexChanged subroutine executes. This subroutine assigns the text from the selected tree node to a Label control named lblSelectedNode.

The TreeViewSelectEventArgs paramter has two properties:

* NewNode - returns a string represneting the index of the selected node.
* OldNode - returns a string representing the index of the previous node

Notice that both the NewNode and OldNode properties return a string, not an integer. I.e., if you select the first book under the ASP.NET Books node, the NewNode property returns the string value &quot;0.0&quot;. If you select the second child node, the property returns &quot;0.1&quot;. The string represents the lcoation of the node in the tree view hierarchy.

In the above example, the GetNodeFromIndex method returns a TreeView node from its index. After you have the node, you can display its label by using the node's Text property.

**** Using Images with TreeNodes (e.g., folders)

<%@ Import Namespace=&quot;Microsoft.Web.UI.WebControls&quot; %>
<%@ Register TagPrefix=&quot;IE&quot; Namespace=&quot;Microsoft.Web.UI.WebControls&quot; Assembly=&quot;Microsoft.Web.UI.WebControls&quot; %>

<html>
<head><title>UsingImageswithTreeView.aspx</title></head>
<body>
<form runat=&quot;server&quot;>

<IE:TreeView
ImageURL=&quot;dir.gif&quot;
ExpandedImageUrl=&quot;dir_open.gif&quot;
ruant=&quot;server&quot;>

<TreeNode
Text=&quot;My favorite URLs&quot;>

<TreeNode
Text=&quot;ASP.NET Books&quot;
NavigateURL=&quot; />

<TreeNode
Text=&quot;VB.NET Books&quot;
NavigateURL=&quot; />

<TreeNode
Text=&quot;ASP.NET Tips&quot;
NavigateURL=&quot; />

</TreeNode>
</IE:TreeView>

</form>
</body>
</html>

If you want to use different images for different nodes you can use the &quot;TreeNodeType&quot; control. Expanding on the last example:

...

<IE:TreeView
ImageURL=&quot;dir.gif&quot;
ExpandedImageUrl=&quot;dir_open.gif&quot;
runat=&quot;server&quot;>

<TreeNode Type
Type=&quot;Document&quot;
ImageUrl=&quot;text.gif&quot;/>

<TreeNode
Text=&quot;ASP Techniques&quot;
Type=&quot;Document&quot;
NavigateURL=&quot;
...

123: I have additional examples using checkboxes, using XML or a database, tabs, etc if you need additonal insight (all from Walther (ASP Unleashed, 2002).

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top