Author’s Introduction

Danish Hameed is a Bachelor’s of science in computer science. He has participated and scored remarkable positions in programming and software competitions. Currently working as Software Engineer in eLearning industry.

Binding Data With ‘TreeView’ Control Asp.net 2.0

Over the period of few weeks I have been trying to play with asp.net 2.0 controls. As most of asp.net 2.0 beginners know there are almost no working examples and tutorials available for beta release and honestly I haven’t found people working on asp.net 2.0 so far as much as I expected.

Now the real thing started when I decided to pick up some pace and started using Microsoft visual studio 2005 asp.net 2.0 instead of VS’s prior version.Among so many new tools the one which I found really fascinating in asp.net 2.0 is TreeView control, the reason is; I have been creating TreeViews on JavaScript and had really tough time integrating them with server requests/responses.

Introduction of TreeView Control

A tree view control displays a hierarchical list of items using lines to connect related items in a hierarchy. Each item consists of a label and an optional bitmap. Windows Explorer uses a tree view control to display directories. It displays a tree structure, or menu, that users can traverse to get to different pages in your site. A node that contains child nodes can be expanded or collapsed by clicking on it.

You can use ASP.NET site Navigation features to provide a consistent way for users to navigate your site. A simple solution is to include hyperlinks that allow users to jump to other pages, presenting the hyperlinks in a list or a navigation menu. However, as your site grows, and as you move pages around in the site, it quickly becomes difficult to manage all of the links.

To create a consistent, easily managed navigation solution for your site, you can use ASP.NET site navigation TreeView control.

Fig: 1.0

Note: you can manually populate this control by, 1)Clicking on TreeView control.2)Right click à Edit Nodes3)Under ‘Nodes’ heading there are two buttons, to add Parentand childnodes respectively.

Lets do some work now!

Now that we have pretty clear understanding of TreeView control, we will quickly move on and integrate this control with our site.

/** Initialize the string ParentNode.* We are going to populate this string array with our ParentTable Records* and then we will use this string array to populate our TreeView1 Control with parent records*/

string[,] ParentNode = new string[100, 2];

/** Initialize an int variable from string array index*/

int count = 0;

/** Now populate the string array using our SQL Datareader Sdr.

* Please Correct Code Formatting if you are pasting this code in your application.*/

/** Now once the array is filled with [Parentid,ParentName]* start a loop to find its child module.* We will use the same [count] variable to loop through ChildTable* to find out the number of child associated with ParentTable.*/

for (int loop = 0; loop < count; loop++)

{

/** First create a TreeView1 node with ParentName and than* add ChildName to that node*/

/** By Default, when you populate TreeView Control programmatically, it expends all nodes.*/ TreeView1.CollapseAll(); SqlCon.Close();

}

Lets see how it looks like!

Fig. 3.0

You can spice it a bit by playing with [autoformat] and [show lines] property.

I will be extremely honored to get feedback from you.

By, Danish Hameed

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

I found the foll article:
Binding Data With ‘TreeView’ Control Asp.net 2.0[^]
by you for tree structure in ASP.Net, but that example has only two levels of folders. I have more than that. In that case how do i make a child folder as a parent folder.
Please share the code for it. I am not able to get it.

Thank you! I tried at least four other methods of populating a TreeView directly from an SQL query -- plus a few others that involved converting to XML data -- and none of them worked. Just plugged this into my code, changed a few lines, and presto!

Hey MS ... if anyone is listening... this was a "wtf" moment in the development community and still remains to be. Seriously no DB access!? We could have used this one out of the box! Perhaps Silverlight will deliver... we will see...

i have a list box control which displays different group names as items. Each group contains multiple group items. I want to display list box items in tree view format.
If anyone knows about it then pleas help.

hi,
i m working on treeview control in asp.net 2005. i am facing a problem, if u knw pls reply of this problem.
My problem is :
i m genrating treeview with 4 parent nodes. each parent nodes have 2 child nodes.If i set the permission of child nodes is 0 then according to me child node as well as parent node should not be display but in treeview child is not display but still parent node is displaying. so i want to know how to remove this problem. All data cming from database (like treeview generation,permission, parentnode, childnode) everything is cming frm database

2)Suggestion :try to make control which has datagrid with tree view features to show child controls data in details instead of just one column and also parent row has summary data ...sth like that may be