How to build an ASP.NET TreeView From Scratch...?
-
I am trying to build a nested/threaded comment system in ASP.NET. I don't know how the PHP guys do it. Its much harder than first imagined. I am trying my damnedest get Hierarchical data to output to the user, but its not working. I have a table with text, a itemID and a parentID. I want to display the information in a tree view format, but asp.net's standard control just doesn't work... Can anyone point me in the right direction of how to output this into a treeview. I have tried nesting repeaters, straight out html building from codebehind and the treeview control. I still haven't found a solution... Can anyone help me out?
-
Answer:
Quickly, by head (could not check in VS2k8 now), I would do it something like this using Linq to SQL private void BuildTree() { List<Item> items = from item in dataContext.Items select item; List<Item> rootItems = items.FindAll(p => p.ParentID == null ); foreach ( Item item in rootItems ) { TreeViewNode tvi = new TreeViewNode(item.text); BuildChildNodes(tvi, items, item.ID); YourTreeNodeName.Nodes.Add(tvi); } } private void BuildChildNodes(TreeViewNode parentNode, List<Item> items, long parentID) { List<Item> children = items.FindAll ( p => p.ParentID = parentID ); foreach( Item item in children) { TreeViewNode tvi = new TreeViewNode(item.text); parentNode.Nodes.Add(tvi); BuildChildNodes(tvi, items, item.ID); } }
SpoiledTechie.com at Stack Overflow Visit the source
Other answers
As I understand it, if you have a database table with hierarchical data, you have two options: create your own custom data source, or programmatically bind the treeview to the database table. I don't have code for you, but you could use following these steps: declare treeview control within asp.net page populate a DataTable (via a SqlDataAdapter) with your heirarchical data (SELECT itemID, parentID FROM...) use that same DataTable to create a DataView of top-level items (parentID will be null) for each row of the DataView, recursively add treeview items by filtering another DataView where each DataViewRow's parentID is equal to the row you're looping through in step 4 pretty much all of this is done using TreeView.Nodes.Add(theNewNode), where theNewNode is an instance of the TreeNode object I know this all sounds very confusing, but I've done it in the past. I found great info in Stephen Walther ASP.NET Unleashed book, which has entire sections devoted to accomplishing this.
alex
Related Q & A:
- How can I allow user to create posts in website using ASP.NET?Best solution by Programmers
- How to Undo previous action in asp.net?Best solution by forums.asp.net
- How to Search using jQuery ASP.Net?Best solution by Stack Overflow
- How to Play Audio files in ASP.NET?Best solution by Stack Overflow
- How to save high resolution image canvas to server using asp.net?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.