Display Hierarchical Data with TreeView in ASP.NET 2.0
page 1 of 1
Published: 31 Oct 2005
Unedited - Community Contributed
Abstract
Teemu Keiski demonstrates how to utilize the new TreeView control in ASP.NET 2.0 to display hierarchical data from a database.
by Teemu Keiski
Feedback
Average Rating: 
Views (Total / Last 10 Days): 318599/ 810

[Download Sample Code]

Now that ASP.NET 2.0 is about to be released to manufacturing, we ASP.NET developers have very interesting times ahead of us. Along with the new Framework, we have a major set of new controls to use and to ease our development work. One of the new controls, also my favorite, is the TreeView control. Microsoft has developed it based on feedback which arose from the release of the TreeView control in the open-source IE Web Controls package, targeted at the previous version of ASP.NET. TreeView has not just been rewritten; it now also includes a major set of new features such as support for client-side populating, on-demand populating, postback events, hyper link navigation, and a lot more.

Sample

I’m going to provide a simple example how to display hierarchical data from SQL Server database in the TreeView. A requirement is that the implementation should not be dependant on the hierarchy level in the database. It means that the TreeView implementation should be capable of displaying data from any level, no matter how deep.

Let’s assume we have TreeViewSampleDB database with the SampleCategories table as follows.

Figure #1

Note: It doesn’t matter if you use SQL Server 2005 or a previous version to try this sample.

Then, we have TreeViewVB.aspx page with the TreeView control declaration as follows.

Listing #1

<asp:TreeView  
  ID="TreeView1"
  ExpandDepth="0" 
  PopulateNodesFromClient="true"
  ShowLines="true" 
  ShowExpandCollapse="true" 
  runat="server" />

This means that we are setting the initial expand depth of the TreeView to the first level of nodes. We want to use the client-side node populating feature so that we can provide our users with a better usage experience. Note that this way, no postbacks are involved. We also want to show lines between the nodes and the expand/collapse icon if our tree nodes have child nodes.

Next, we start looking at the code in the TreeViewVB.aspx.vb file. We want to populate the root level. Here’s the code.

Listing #2

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  If Not Page.IsPostBack Then
    PopulateRootLevel()
  End If
End Sub
 
Private Sub PopulateRootLevel()
  Dim objConn As New SqlConnection(_
    "server=JOTEKE\SQLExpress;Trusted_Connection=true;DATABASE=TreeViewSampleDB")
  Dim objCommand As New SqlCommand("select id,title,(select count(*) FROM SampleCategories " _
    & "WHERE parentid=sc.id) childnodecount FROM SampleCategories sc where parentID IS NULL", _
    objConn)
 
  Dim da As New SqlDataAdapter(objCommand)
  Dim dt As New DataTable()
  da.Fill(dt)
 
  PopulateNodes(dt, TreeView1.Nodes)
End Sub

This happens by connecting to the database, querying the first set of nodes (having null as the parent id), and creating TreeNode objects with the PopulateNodes routine, which follows next.

Listing #3

Private Sub PopulateNodes(ByVal dt As DataTable, _
  ByVal nodes As TreeNodeCollection)
  For Each dr As DataRow In dt.Rows
    Dim tn As New TreeNode()
    tn.Text = dr("title").ToString()
    tn.Value = dr("id").ToString()
    nodes.Add(tn)
 
    'If node has child nodes, then enable on-demand populating
    tn.PopulateOnDemand = (CInt(dr("childnodecount")) > 0)
  Next
End Sub

Here’s an important piece of logic. The PopulateNodes method iterates through all rows in the DataTable, and if the given node has child nodes, it sets the PopulateOnDemand property of the TreeNode object according to that. This has the effect that the expand/collapse icon is shown if the node has child nodes.

Next, we want to create the routine to populate the child nodes of a given node. This happens with the PopulateSubLevel method.

Listing #4

Private Sub PopulateSubLevel(ByVal parentid As Integer, _
  ByVal parentNode As TreeNode)
  Dim objConn As New SqlConnection(_
    "server=JOTEKE\SQLExpress;Trusted_Connection=true;DATABASE=TreeViewSampleDB")
  Dim objCommand As New SqlCommand("select id,title,(select count(*) FROM SampleCategories " _
    & "WHERE parentid=sc.id) childnodecount FROM SampleCategories sc where parentID=@parentID", _
    objConn)
  objCommand.Parameters.Add("@parentID", SqlDbType.Int).Value = parentid
 
  Dim da As New SqlDataAdapter(objCommand)
  Dim dt As New DataTable()
  da.Fill(dt)
  PopulateNodes(dt, parentNode.ChildNodes)
End Sub

Here, the idea is the same as with the root level, but with the distinction that only child nodes of the given node are queried and populated with the PopulateNodes method (described earlier). The trick to triggering the populating of the child nodes is as follows.

Listing #5

Protected Sub TreeView1_TreeNodePopulate(ByVal sender As Object, _
  ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles TreeView1.TreeNodePopulate
  PopulateSubLevel(CInt(e.Node.Value), e.Node)
End Sub

That is, when the TreeView’s TreeNodePopulate event is raised, PopulateSubLevel is called.

TreeNodePopulate is raised for a TreeNode which is expanded by the user for the first time. Due to PopulateNodesFromClient (TreeView) and PopulateOnDemand (TreeNode) settings, this happens with the client-side callback mechanism which is handled by the ASP.NET Page framework. This means that populating does not involve a postback. And, due to better cross-browser support in ASP.NET 2.0, this also works for other browsers such as Firefox. Note that clicking on the node does cause a postback because I haven’t modified the select action of the populated tree nodes from the defaults.

With this setup, trying our sample page and expanding the nodes, we should get a view like this.

Conclusion

We have set up a working TreeView relatively easily. This approach requires a little bit of code, but shows that it’s not really because of the TreeView control but due to the nature of hierarchical data. Certainly, there’s room for creating a hierarchical data source control to deal with this so that data access wouldn’t require that much code; however, I believe that getting your hands in code is the best way to learn about the technology.



User Comments

Title: Thanks.......   
Name: Biswajit
Date: 2/4/2013 1:51:53 AM
Comment:
thanks a lot...........
this really helped me a lot.
Title: just get some nodes   
Name: SMH
Date: 1/29/2013 1:10:01 AM
Comment:
hi & thanks
i want get some nodes from all of the data in may table
for example my table has 100 items and i want get items that have ID > 40
how change the query in your sample?
please help me
Title: ERROR: Specified Cast is not valid   
Name: Melvin
Date: 1/9/2013 8:45:08 PM
Comment:
..continue
private void PopulateNodes(DataTable dt, TreeNodeCollection nodes)
{
foreach (DataRow dr in dt.Rows)
{
TreeNode tn = new TreeNode();
tn.Text = dr["title"].ToString();
tn.Value = dr["id"].ToString();
nodes.Add(tn);

//If node has child nodes, then enable on-demand populating
tn.PopulateOnDemand = ((int)(dr["childnodecount"]) > 0); //ERROR: Specified Cast is not valid at this line, please help

}
}
Title: ERROR: Specified Cast is not valid   
Name: Melvin
Date: 1/9/2013 8:41:46 PM
Comment:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
PopulateRootLevel();
}

private void PopulateRootLevel()
{
string connectionString = ConfigurationManager.ConnectionStrings["csTest"].ToString();

MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection(connectionString);
MySql.Data.MySqlClient.MySqlCommand cmd = conn.CreateCommand();

cmd.CommandText = "SELECT id,title,(SELECT COUNT(*) FROM test.treeview WHERE parentid=sc.id) childnodecount FROM test.treeview sc WHERE parentID IS NULL";

MySql.Data.MySqlClient.MySqlDataAdapter da = new MySql.Data.MySqlClient.MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
PopulateNodes(dt, TreeView1.Nodes);
}

private void PopulateSubLevel(int parentid, TreeNode parentNode)
{
string connectionString = ConfigurationManager.ConnectionStrings["csTest"].ToString();

MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection(connectionString);
MySql.Data.MySqlClient.MySqlCommand cmd = conn.CreateCommand();

cmd.CommandText = "SELECT id,title,(SELECT COUNT(*) FROM test.treeview WHERE parentid=sc.id) childnodecount FROM test.treeview sc WHERE parentID=@parentID";

cmd.Parameters.Add("?parentID", MySqlDbType.Int32, parentid);
MySql.Data.MySqlClient.MySqlDataAdapter da = new MySql.Data.MySqlClient.MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
PopulateNodes(dt, parentNode.ChildNodes);
}

protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
PopulateSubLevel(Int32.Parse(e.Node.Value), e.Node);
}
Title: Officer   
Name: Rowshan
Date: 1/8/2013 10:30:09 PM
Comment:
fine
Title: just get some nodes   
Name: SMH from IRAN
Date: 12/23/2012 3:37:49 PM
Comment:
hi & thanks
i want get some nodes from all of then data in may table
for example my table has 100 items and i want get items that have ID > 40
how change the query in your sample?
sorry i have problem in english language
Title: The name 'TreeView1' does not exist in the current context   
Name: Dan
Date: 11/1/2012 11:08:28 PM
Comment:
Great work and I see that it is still helping people after 6 years. I'm using VS2012-C# and had to make one adjustment to your code to avoid the error [The name 'TreeView1' does not exist in the current context.] I see others have gotten this at times too, and it is difficult for new users to troublehsoot. I had to enclose the content[not the using statments] of the code behind page in a namespace where the namespace was the project name. Then it all worked.
Title: Please help me...   
Name: Angel Kate
Date: 10/2/2012 10:46:05 PM
Comment:
\S
\S
Title: Please help me.   
Name: Angel Kate
Date: 10/2/2012 10:40:06 PM
Comment:
\S
\S
Title: Thanks   
Name: B.
Date: 9/18/2012 9:17:26 AM
Comment:
Thanks, this really helped me a lot.
Title: Great   
Name: Guilherme
Date: 8/17/2012 1:11:48 PM
Comment:
Great post!!!
Title: Fantastic stuff   
Name: David
Date: 8/9/2012 2:19:36 AM
Comment:
Have been looking for a way to do 3 level menus for a while, as I had found some code that could do 2 levels, but wasn't easily adaptable for 3 levels. Was able to adapt this to suit a horizontal menu as well as horizontally arranged treeviews. With this code and data, I can see that I can do as many levels in my menus/treeviews as I like, based around the parent id settings for each option. Working like a charm. Thanks heaps.
Title: irfi   
Name: dhsu
Date: 8/9/2012 1:21:20 AM
Comment:
hggsdhs
Title: Not work with Masterpage   
Name: tnb
Date: 7/12/2012 12:42:02 AM
Comment:
my treeview is not in a master page...can any body tell why and code example too..
Title: Thanks yarr   
Name: Manas Ranjan
Date: 7/3/2012 7:02:12 AM
Comment:
Its working Fantastic ,excellent.I have many ways nothing work perfectly.Just convert vb.net to C#.net.In VB
'CInt' in C#.net convert.toint32.thanks.......
Title: Thanks   
Name: Vaishali
Date: 6/26/2012 8:24:29 AM
Comment:
Thanks.....
helped a lotttt.........
Title: Great.!   
Name: venkatswamy
Date: 6/18/2012 11:09:43 AM
Comment:
Thanks.. for the Logic...
Title: Thanks   
Name: Neetesh
Date: 5/31/2012 4:58:19 AM
Comment:
Thanks...............
Title: Outstanding Job Sir!   
Name: pnoneal
Date: 4/16/2012 6:52:46 PM
Comment:
I had to convert your code to C#, but that was easy except for two problems I had. One is C# doesn't have the CInt data type. I spent a good hour searching MSDN for CInt in C# but all the articles were in VB so I guess C# doesn't have one. Once I concluded that, I just type casted it to an Int32. In another one, I had to use the C# funcion Convert.ToInt32().

The functions work to populate a TreeView with the table you layed out. The only thing I need to do is add a column to mine with the navigateURL string since I'm using the TreeView as my menu for my website.

So thank you very much.

Paul
Title: Great! Here's an update on SqlCommand...   
Name: Lightbulb
Date: 3/28/2012 11:43:15 PM
Comment:
SqlCommand objCommand = new SqlCommand(
"SELECT id, description, (SELECT COUNT(*) AS Expr1 " +
"FROM navigation_list " +
"WHERE (parent_id = sc.id)) AS childnodecount " +
"FROM navigation_list AS sc " +
"WHERE (parent_id = @parentID)", objConn);

and you should close your connection on each request...
objConn.Close();
Title: it's working Good and very help full for me   
Name: Ubaid ur Rahman
Date: 3/22/2012 6:09:09 AM
Comment:
thank you your code very very very help me and i want to thanks of you bundle and bundle thanks because i was confused about this and finally i solve and issue from your code. it's very help full for me.

thanks alots.
Title: its not work...child not not display..require...   
Name: sonali
Date: 3/8/2012 1:56:54 AM
Comment:
ID="TreeView1"
ExpandDepth="0"
PopulateNodesFromClient="true"
ShowLines="true"
ShowExpandCollapse="true"
OnTreeNodePopulate="TreeView1_TreeNodePopulate"
runat="server" Height="361px" Width="338px" />
Title: Tree view node   
Name: Ayesha
Date: 3/5/2012 7:43:05 AM
Comment:
PopulateSubLevel(Int32.Parse(e.Node.Value), e.Node);
this is creating error in my code
METHOD ARGUMENTS ARE NOT IN RIGHT FORMAT
kindly tell me why this happening?
Title: vertical treeview   
Name: sanjay gajera
Date: 1/6/2012 2:57:07 AM
Comment:
can we do the same tree view control in vertical manner???
reply as soon as possible.
Title: Use the code download   
Name: Teemu
Date: 11/28/2011 1:59:06 AM
Comment:
Use the code download, it has both VB.NET and C#.
Title: About Article   
Name: yamuna
Date: 11/27/2011 1:12:15 PM
Comment:
sir, excellent job..u have done...but ineed the coding in c#.....can u help me
Title: Need horigental Tree   
Name: Avinash
Date: 10/8/2011 12:15:49 PM
Comment:
i am looking for horizantal binary tree.
Title: website design company   
Name: website
Date: 10/3/2011 11:50:25 AM
Comment:
Nice post but I have to develop a MLM project. can you help me.
Title: nice one example   
Name: Sunil
Date: 9/22/2011 4:11:15 AM
Comment:
nice one example
Title: childnodes not coming   
Name: dhanya
Date: 7/18/2011 12:21:34 AM
Comment:
below is my doubt.
if any on knows,please help me...
my mail id is dhnk009@gmail.com
Title: child nodes are not coming   
Name: dhanya
Date: 7/18/2011 12:19:20 AM
Comment:
hi, I tried this code.but only root node is coming.I am a beginner only in asp.net and sql server.
below is the code i tried.

protected void Page_Load(object sender, EventArgs e)
{


if (!Page.IsPostBack)
{
PopulateRootLevel();

}

}

private void PopulateRootLevel()
{
SqlConnection outputconn = new SqlConnection();
outputconn.ConnectionString = ConfigurationManager.ConnectionStrings["sqloutputConnString"].ConnectionString;
SqlCommand objCommand = new SqlCommand(@"select id,title,(select count(*) FROM example WHERE parentid=e.id) childnodecount FROM example e where parentID IS NULL", outputconn);
SqlDataAdapter da = new SqlDataAdapter(objCommand);
DataTable dt = new DataTable();
da.Fill(dt);
PopulateNodes(dt, TreeView1.Nodes);
outputconn.Close();
}

private void PopulateSubLevel(int parentid, TreeNode parentNode)
{
SqlConnection outputconn = new SqlConnection();
outputconn.ConnectionString = ConfigurationManager.ConnectionStrings["sqloutputConnString"].ConnectionString;
SqlCommand objCommand = new SqlCommand(@"select id,title,(select count(*) FROM example WHERE parentid=e.id) childnodecount FROM example e where parentID=@parentID", outputconn);
objCommand.Parameters.Add("@parentID", SqlDbType.Int).Value = parentid;
SqlDataAdapter da = new SqlDataAdapter(objCommand);
DataTable dt = new DataTable();
da.Fill(dt);
PopulateNodes(dt, parentNode.ChildNodes);
outputconn.Close();
}


protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
PopulateSubLevel(Int32.Parse(e.Node.Value), e.Node);
}

private void PopulateNodes(DataTable dt, TreeNodeCollection nodes)
{
foreach (DataRow dr in dt.Rows)
{
TreeNode tn = new TreeNode();
Title: Thanks   
Name: Ali
Date: 7/3/2011 1:24:39 AM
Comment:
Hello
Thanks For your Code

Best Regards
پروژه هاي دانشجويي
www.bitasoft.ir
09131253620
Title: gr8   
Name: sandeep
Date: 6/20/2011 5:23:23 AM
Comment:
thanks for this perfect code.

now i want to add removebutton,changepositionbutoon,addnewnode or childnode button
Title: getting post backs   
Name: priya
Date: 6/20/2011 2:33:24 AM
Comment:
hey,

On clicking the node it is getting postback and the child nodes are not opening.

any one plz give suggesstions

thanks
Title: thanks Very good content   
Name: swapnil k.
Date: 6/6/2011 6:55:51 AM
Comment:
hey....

Thanks Dude

it works great.....

Very clear and good explanation

Thanks Again

You are rocking :)
Title: Very Good Idea   
Name: Anbu
Date: 5/26/2011 6:37:41 AM
Comment:
How to Bind a gridview like a Treeview.Can we use the same concept
Title: Oh hey men. Good Job!   
Name: tritoan
Date: 5/20/2011 5:48:36 AM
Comment:
Thanks!
It save my time.
Very good!!!!!!!!!!
Title: Display this Horizontal   
Name: PRABu
Date: 5/14/2011 3:15:02 AM
Comment:
How to display in Horizontal view?
Title: Excellent Article   
Name: Rashid
Date: 5/4/2011 9:03:36 AM
Comment:
Thank you very much for this post. Short and sweet solution :)
Title: i'm from Mongolia   
Name: Tengis
Date: 4/5/2011 8:32:47 AM
Comment:
Thanks, your idea is great. Here is the C# code
private void PopulateRootLevel()
{
try
{
SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlCommand Command = new SqlCommand("SELECT DepartmentID,DepartmentName," +
"childnodecount = (SELECT Count(*) FROM Department WHERE ParentDepartmentID = D.DepartmentID) "+
"FROM Department D WHERE ParentDepartmentID IS NULL" ,Conn);
SqlDataAdapter da = new SqlDataAdapter(Command);
DataTable dt = new DataTable();
da.Fill(dt);
PopulateNodes(dt, TreeView1.Nodes);
}
catch (Exception ex)
{}
}

private void PopulateNodes(DataTable dt,TreeNodeCollection nodes)
{
try
{
foreach (DataRow dr in dt.Rows)
{
TreeNode tn = new TreeNode(dr["DepartmentName"].ToString(),dr["DepartmentID"].ToString());
nodes.Add(tn);
tn.PopulateOnDemand = (int.Parse(dr["childnodecount"].ToString()) > 0 );
}
}
catch(Exception ex)
{}
}

private void PopulateSubLevel(int parentid,TreeNode parentNode)
{
try
{
SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlCommand Command = new SqlCommand("SELECT DepartmentID,DepartmentName," +
"childnodecount = (SELECT Count(*) FROM Department WHERE ParentDepartmentID = D.DepartmentID) "+
"FROM Department D WHERE ParentDepartmentID = @parentID" ,Conn);
Command.Parameters.AddWithValue("@parentID",parentid);

SqlDataAdapter da = new SqlDataAdapter(Command)
Title: You are great   
Name: sara
Date: 4/1/2011 4:41:30 PM
Comment:
your technic is very practical and useful.

Thanks :)
Title: Thank you So Much   
Name: Afsur Hussain
Date: 3/15/2011 11:46:19 PM
Comment:
Hi this is awesome.
I am very happy after getting ur code.
Thank you very much
Title: Working Great   
Name: sandip
Date: 3/11/2011 6:16:44 AM
Comment:
This solution works great! but how to make first node in tree as root node problematically. Any one have solution plz reply to this post.

thanks in advance
Title: Very good solution   
Name: upendra kumar
Date: 3/10/2011 8:59:59 AM
Comment:
Easy to Impliments all guys
Title: Tree View   
Name: Maks
Date: 3/2/2011 8:14:16 AM
Comment:
Thanks!!!
Title: probelm   
Name: Bishnu Biswal
Date: 2/24/2011 5:32:03 AM
Comment:
when data save database using treeview data not populate treeview until refesh button click help me
Title: Fantastic   
Name: Bishnu Biswal
Date: 2/22/2011 6:41:54 AM
Comment:
Thank you so much for a nicely written and explained solution
Title: Great!   
Name: Jhing
Date: 2/14/2011 5:00:05 AM
Comment:
Thanks! This is very simple to understand. You're Great!
Title: Display Hierarchical Data with TreeView in ASP.NET 2.0Display Hierarchical Data with TreeView in ASP.NET 2.0   
Name: wasim
Date: 2/9/2011 4:11:37 PM
Comment:
u can download code. In code there is c# cs file
Title: Display Hierarchical Data with TreeView in ASP.NET 2.0Display Hierarchical Data with TreeView in ASP.NET 2.0   
Name: Karthik
Date: 2/3/2011 4:26:01 AM
Comment:
Can u do same code in c#.i need in asp.net not in vb
Title: Tree view control   
Name: jayesh
Date: 1/1/2011 5:30:28 AM
Comment:
Nice article man ..
You have done great job.
Best article for bind sql to Tree View
Title: the best TreeView from DB article   
Name: Ahmed
Date: 12/27/2010 4:28:07 PM
Comment:
This is the best TreeView from DB code.
Thanks,
Ahmed.
Title: Awesome   
Name: Parimal Modi
Date: 12/23/2010 2:55:48 AM
Comment:
working Great!!!
Title: Fantastic , Awesome   
Name: Arindam Basu
Date: 12/3/2010 4:27:46 PM
Comment:
One of the best articles I have come across so far on implementng hierarchial tree view.

Thanks a lot for posting this article. It's very useful for me.
Title: Well explained   
Name: Estevao
Date: 11/22/2010 4:45:25 PM
Comment:
Opens my mind to understand all other good articles about this matter
Title: Good Job   
Name: Ahmed
Date: 11/10/2010 6:14:38 AM
Comment:
Thanks Teemu,
This is the best code I've found for displaying hierarchical data from a database.
Title: NAF   
Name: Georges
Date: 10/28/2010 5:13:54 AM
Comment:
great code , how can we (link to) I mean lets say I want to do like msdn you click on a node in the tree and you see it on the right any help with that ?
Title: How to get the Id of selected node   
Name: Himanshu Sharma
Date: 10/1/2010 5:16:09 PM
Comment:
Hi,

Your article is very good and is really helpful to me. It would be great if you could tell me how to fetch the ID of the selected node like when we click on any node we can get the ID of that particular node.

Thanks in advance...
Title: Thank you   
Name: mike
Date: 9/27/2010 7:25:44 AM
Comment:
working Great!!!
Title: Great   
Name: Amod L
Date: 9/24/2010 1:03:32 AM
Comment:
This is gr8.
I have implemented this in user control and I am overriding page render event to move viewstate at the bottom..
then TreeView1_TreeNodePopulate event not working
any idea?
Title: Tree View   
Name: SHAJAN
Date: 9/20/2010 3:12:23 AM
Comment:
Very useful. Thank you verymuch
Title: Nested gridview in vb.net windows forms   
Name: ITS
Date: 9/8/2010 3:37:01 AM
Comment:
I need nested gridview in vb.net windows forms
Title: mjb   
Name: mark
Date: 8/19/2010 7:08:21 PM
Comment:
This i can do, how about when your database records change and you need to repopulate the TreeView with the changes.
Title: tnks   
Name: eegii
Date: 8/10/2010 3:29:56 AM
Comment:
thank you very much.
Title: Please solve my query   
Name: Sagar
Date: 7/31/2010 2:16:52 AM
Comment:
I have bind treeview with database in C#.But i want add node to treeview from the front end.
Problem-:
A)I have added one button on top.Then i am going to select any node from treeview .After clicking on ADD button i will get 2 options 1.ABOVE 2.BELOW
If i click on ABOVE, new node will be added above the selected node of tree view and i click on BELOW, new node will be added BELOW the selected node of tree view and that changes also effect in database


B)I want delete selected node after click on delete button and that changes also effect in database

Please its urgent!!!!!
sagarmalbari@gmail.com
Title: can i get it in C# sir   
Name: Sudheer
Date: 7/30/2010 1:21:21 AM
Comment:
hello sir
i have the same structure in Database Please let me know this in C# can mail me at talk2myheart@gmail.com
Title: Very Nice Article   
Name: Venkat
Date: 7/16/2010 1:22:55 AM
Comment:
Thanks a lot!
Title: TreeView won't expand so please do like this it works for me!   
Name: Sin Savada
Date: 6/28/2010 5:31:21 AM
Comment:
PopulateRootLevel();
TreeView1.ExpandAll();
TreeView1.CollapseAll();
Title: Very great topic   
Name: Sin Savada
Date: 6/16/2010 3:53:17 AM
Comment:
Thanks a lot!
Title: Thaks   
Name: Niranjan Awale
Date: 6/6/2010 2:39:35 AM
Comment:
great hlp .Thou found lately
Thankx Teemu!
Title: Great article!   
Name: blasouveir
Date: 5/26/2010 5:44:41 AM
Comment:
Thanks a lot! I managed to modify this example to create multi level acordion menu. Its been very helpfull!
Title: navigate on pages   
Name: malik umar
Date: 5/25/2010 10:57:41 PM
Comment:
how we navigate on aspx pages using above code kindly help me
its very urgent

malikumarhassan@live.com
Title: Excellent explanation   
Name: Tony
Date: 5/24/2010 3:59:07 AM
Comment:
Now I am working out how to add/update/remove nodes including manipulating sql server table. I believe it will be workable.
Thank you again.
Title: Oracle Database   
Name: Maria
Date: 4/16/2010 5:10:22 AM
Comment:
Please let me know what changes I need to make to this code if I am connecting to an Oracle Db.
Title: TreeView won't expand   
Name: ga
Date: 4/12/2010 7:07:09 AM
Comment:
Same here even my treeview is not in a master page...
Title: TreeView won't expand   
Name: JA
Date: 4/5/2010 12:28:22 PM
Comment:
Greetings,

I'm using .NET 3.5, and followed this example. I even made a sample table with the same data. However, when I view it in browser, the treeview only shows Category 1, 2 and 3, and will not expand when clicked. Any ideas? This happens even if I set the populateNodEvent to TreeView1_TreeNodePopulate. My treeview is in a master page. Any ideas? Thanks in advance.
Title: Just what i was looking for   
Name: KC Abramson
Date: 4/2/2010 9:30:16 PM
Comment:
Excellent work! Thank you so much.
Title: Very Useful code   
Name: Himanshu Sharma
Date: 3/30/2010 7:54:48 AM
Comment:
Hi,

Thank you very much,this is a very useful piece of code to populate a treeview.
Title: Very Useful Article   
Name: Bhavin Joshi
Date: 3/30/2010 3:29:21 AM
Comment:
Hi,

This is very useful article. I must thank you as I was searching for such solution from no. of days.
Title: Good Article   
Name: sameer.gangavathi@gmail.com
Date: 3/26/2010 4:19:54 PM
Comment:
Sir,

Thanks for this article, I have struck up with last week, due to your solution, I have achived my result.

Thanks
Shameer
Bangalore,India.
Title: Tree view   
Name: Ketan Dave
Date: 3/12/2010 1:35:53 AM
Comment:
I tried this code. but when query was fired it generates error like "incorrect syntax near id"
here query like
(@"select id,title,(select count(*) FROM SampleCategories WHERE parent id=sc.id) childnodecount FROM SampleCategories sc where parent id=@parent id",objConn);
pls reply me
Title: PS on my 2/5 post   
Name: Torian
Date: 2/19/2010 11:49:23 AM
Comment:
Further understanding by reading your article and working with the code has helped me resolve the original issue.
My 2/19 issue still remains (Title 'Fantastic - Adaptation?Phase2)
If every great programmer wrote articles like these, it would be SO helpful to us programmer-wannabe's with such extreme challenges. :D
Title: Fantastic - Adaptation? Phase 2   
Name: Torian
Date: 2/19/2010 11:45:26 AM
Comment:
I now have 2 Root Level Nodes set manually - SpouseOneAndKids for the first mother and child in my genealogy program, and OtherSpousesAndKids for all the others and have set the mothers as a child node to the text titles. I have modified the code to get the mothers' names but now can't figure out how to populate the sub levels (which are the mothers). Technically, both the mother names AND children are now sublevels, but it still takes me 2 queries to get that information (Mother being SampleCategories and Children being Products). Wish I could figure this out, this article is the BEST one I've found (and I've spent HOURS, no, days -- finding this one!)
So, technically, I'm looking for a way to manually create level one and sub-populate accordingly. Is that impossible?

For example: what if you had a table or listing a level ABOVE SampleCategories? That would be my 'First Spouse' and 'Other Spouse' levels.
Thanks again. I know this is the code that will ultimately work for me.
Title: Tree View   
Name: Qamer
Date: 2/17/2010 1:27:42 AM
Comment:
Fantastic!!!!!
Title: code in c# (...contd)   
Name: manju
Date: 2/9/2010 11:02:36 PM
Comment:
cmd.Parameters.Add("@parentID", SqlDbType.Int).Value = parentid;

SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
PopulateNodes(dt, parentNode.ChildNodes);
}


protected void TreeView1_TreeNodePopulate1(object sender, System.Web.UI.WebControls.TreeNodeEventArgs e)
{
PopulateSubLevel(int.Parse(e.Node.Value), e.Node);
TreeView1.CollapseAll();
}
}
Title: code in c#   
Name: manju
Date: 2/9/2010 10:59:55 PM
Comment:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Web.Configuration;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
PopulateRootLevel();
}

}
private void PopulateRootLevel()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["samantha"].ToString());
SqlCommand cmd = new SqlCommand("select id,title,(select count(*) FROM categories " + "WHERE parentid=sc.id) childnodecount FROM categories sc where parentID IS NULL", con);

SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);

PopulateNodes(dt, TreeView1.Nodes);
}
private void PopulateNodes(DataTable dt, TreeNodeCollection nodes)
{
foreach (DataRow dr in dt.Rows)
{
TreeNode tn = new TreeNode();
tn.Text = dr["title"].ToString();
tn.Value = dr["id"].ToString();
nodes.Add(tn);
tn.NavigateUrl = "http://www.google.com";

//If node has child nodes, then enable on-demand populating
tn.PopulateOnDemand = ((int)dr["childnodecount"] > 0);
}
}

private void PopulateSubLevel(int parentid, TreeNode parentNode)
{

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["samantha"].ToString());
SqlCommand cmd = new SqlCommand("select id,title,(select count(*) FROM categories " + "WHERE parentid=sc.id) childnodecount FROM categories sc where parentID=@parentID", con);
cmd.Parameters.Add("@parentID", SqlDbType.Int).Value =
Title: the above code i want in asp.net with c#   
Name: charan
Date: 2/6/2010 3:31:27 AM
Comment:
the above code i want in asp.net with c#
Title: Fantastic - Adaptation?   
Name: Torian
Date: 2/5/2010 6:24:46 PM
Comment:
What an excellent article! I have been trying to 'mod' it a bit to suit my purposes. I want to ACTUALLY display a mother (parent) with all her children on her SPOUSE'S page, and if that man has more than one wife, display all the wives with all their children. Seriously.
The problem is for my purposes that the sub proc actually works too (thank you, will use it elsewhere) BUT since I'm working with genealogy, I really don't want all those children-of-children-of-children coming from the database.

Is there a way to rearrange this so that not only does only one parent with their children (or 2 spouses with their children) display without actually going to the db and GETTING all those leaves and twigs? :D

EVERYONE with a parent has a parentID in my table, so when I do this it actually works (using ID instead of NULL), but then I get all her children, grandchildren, great grandchildren, etc.) Don't want the db doing that much work.

Thanks for a SUPERB ARTICLE.
Title: Good Work   
Name: Khushbu Dave
Date: 2/4/2010 11:22:00 PM
Comment:
Thanks...Its Help me..
Title: It works   
Name: Hardik Gheewala
Date: 2/4/2010 7:18:10 AM
Comment:
Thanks buddy. Its really help me
Title: Correction   
Name: Syed Asif Ali
Date: 2/3/2010 5:21:52 PM
Comment:
Its Horizental Not Vertical.
Title: Imprasive   
Name: Syed Asif Ali
Date: 2/3/2010 5:20:45 PM
Comment:
Its good but can any budy help me to make Vertical Binary Tree with or with out free Tree Controls. I try Fly Fly Tree View but its not free does any budy have the solution????????????

And thanks to Above Code Writter/ Logic Bulder.
Title: Good   
Name: Mohammad
Date: 2/3/2010 4:22:37 AM
Comment:
Thanks for this code it's help me a lot!
Title: Tnx!   
Name: Rob
Date: 2/1/2010 7:32:10 AM
Comment:
Thank you!
Title: Expand Root   
Name: James
Date: 1/22/2010 3:35:38 PM
Comment:
Is there a way to have the root expanded when the tree first loads? I've tried everything.
Title: Treeview to Database   
Name: jibjib89
Date: 1/18/2010 4:24:34 AM
Comment:
Thank you very much.
and here is code for add Treeview to Database

Private Sub GetAllChildren(ByVal parentNode As TreeNode, ByVal strParentID As Integer)
For Each childNode As TreeNode In parentNode.ChildNodes
Dim parentID As Integer
parentID = InsertNode(childNode.Value, strParentID)
GetAllChildren(childNode, parentID)
Next
End Sub
Title: best one   
Name: rashmi
Date: 1/7/2010 8:02:14 AM
Comment:
gr8 example and explanation.....thank u......
Title: Great   
Name: Bharath
Date: 1/5/2010 9:38:21 AM
Comment:
Really great work. Thank you very much.

Bharath.
Title: Very nice   
Name: Vicky
Date: 12/28/2009 8:41:53 AM
Comment:
Code is very nice, worked well.
But if i want to open a another page on clicking child node then how will i check that now there is not another child in that node and what will be code, which can be used with above code.

Thanks
Please Reply
Title: error occur   
Name: anurag
Date: 12/18/2009 4:44:42 AM
Comment:
i m using two table.
1st has field id,name(menu) tablename is product
2nd has field id(same as 1st field),name(submenu) tablename is plans.
in the populatesublevel i use query like this
("select name,(select count(*) FROM plans where id=product.id) as childnodecount FROM product where ID=@id"
then tn.Value = dr("Id").ToString() error occur when control goes into populatenode again.
plz help me out.
Title: Re: error occur   
Name: Ryan
Date: 12/18/2009 3:08:58 AM
Comment:
If you are using C#, use this code
tn.Value = dr["Id"].ToString();
Make sure that your table has a column called 'Id'
Title: error occur   
Name: anurag
Date: 12/17/2009 5:21:15 AM
Comment:
a error occur when control goes into populatesublevel.
this is given below at line.
tn.Value = dr("Id").ToString()

error is Column 'Id' does not belong to table .
please help me out.
Title: Problem with Sample Code   
Name: M. Liening
Date: 12/15/2009 7:28:02 AM
Comment:
Hi,
there is a problem with the sample code. The Errormessage is: "The handles-clause needs a withevents-variable".
Title: Node.Parent is null problem   
Name: Nik Marshall-Blank
Date: 12/14/2009 5:30:30 AM
Comment:
For those who have the problem of the Node.Parent being null when populating on demand set the PopulateNodesFromClient to false.
Title: Do not show leaf items   
Name: ryan
Date: 12/8/2009 6:02:02 AM
Comment:
Hi Teemu,

How can i only the parent nodes? I do not want to show the leaf nodes. I am using this to build a shopping cart, and at the moment, all the items are in the tree view. What i want is to select a product type from the tree view then the actual product in my dropdown list where the user can then add the item to his/her shopping cart.

Help would be most welcome
Title: Can u introduce some Content management System based on this database   
Name: Peter Hon
Date: 11/20/2009 10:29:11 PM
Comment:
Hi,
The code is very useful for me. But I want to build a simple content management system on your databse. Can u introduce some information on how to build a CMS on your
example. Thanks
Title: ASP.Net Tree Menu   
Name: Dharmendra Singh Rajput
Date: 11/20/2009 4:54:21 AM
Comment:
Hi
It is very nice article, But I want to give Links to the each nodes. Can u help me? I will be very thankful, If u halp me.

Thanks in Advance... :)
Title: Need ideas on varation of this   
Name: GS
Date: 11/16/2009 1:46:15 PM
Comment:
nice article, but I am looking for a variation of this.
I am looking for a hierarchical control where each row can be expanded to show a table of values(nesting levels can be 6 or more). I achieved this by defining the number of nested repeater controls in the aspx page but I would like to do this dynamically based on the number of nesting in the data (say a list source). Can I use the treeview control to display a table at each node level ?? Any ideas on this would be great. Thanks. ~GS
Title: hyperlink to the nodes   
Name: Ryan
Date: 11/5/2009 9:19:20 AM
Comment:
Hi Anitha, Soz for the long wait. What Error is it giving you?
Title: What's the problem with the code download?   
Name: Teemu
Date: 10/26/2009 2:44:16 AM
Comment:
See top of the page "Download Sample Code"
Title: C# code for this program   
Name: Somnath
Date: 10/26/2009 2:27:18 AM
Comment:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class tree : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
PopulateRootLevel();
}
}



private void PopulateRootLevel()
{
SqlConnection objConn = new SqlConnection("Data Source=localhost;Trusted_Connection=true;DATABASE=swabhiman;user id=sa;password=sa;");
SqlCommand objCommand = new SqlCommand("select id,name,(select count(*) FROM commissioned_detail WHERE introducer_id=sc.id) child FROM commissioned_detail sc where introducer_id ='01900100' order by name", objConn);

SqlDataAdapter da = new SqlDataAdapter(objCommand);
DataTable dt = new DataTable();
da.Fill(dt);
PopulateNodes(dt, TreeView1.Nodes);
}


private void PopulateNodes(DataTable dt, TreeNodeCollection nodes)
{
foreach (DataRow dr in dt.Rows)
{
TreeNode tn = new TreeNode();
string name = dr["name"].ToString();
string id = dr["id"].ToString();
string com = name + '(' + id+')';
//tn.Text = dr["name"].ToString();
tn.Text = com;
tn.Value = dr["id"].ToString();
nodes.Add(tn);

//If node has child nodes, then enable on-demand populating
tn.PopulateOnDemand = ((int)dr["child"] > 0);
}
}


private void PopulateSubLevel(int parentid, TreeNode parentNode)
{
SqlConnection objConn = new SqlConnection("Data Source=localhost;Trusted_Connection=true;DATABASE=swabhiman;user id=sa;password=sa;");
SqlComman
Title: Good article but i need code in C#   
Name: Somnath
Date: 10/26/2009 1:02:40 AM
Comment:
I liked the post.it is really good one .but it will be better if you can give the codes in C#.

By the way Thanks
Title: Incredible Article !!   
Name: malika
Date: 10/22/2009 8:40:22 AM
Comment:
It saved me a lot of time
thanks you very much
Title: hyperlink to the nodes   
Name: Anitha
Date: 10/15/2009 4:02:56 PM
Comment:
Hi Ryan,
Thanks a lot for the idea and suggestion. I am new to ASP.net ... I tried your code but it gives me errors .... can you Please show me how I can pass the value to the query and again retreieve the value for opening the file from a folder ??? .. All your help will be very much appreciated .... Once again Thank you so very much Ryan ...

Anitha
Title: add a node at any leval   
Name: Ryan
Date: 10/12/2009 4:51:47 AM
Comment:
Hey sam,

could you please explain what you want to do?
Title: Awesome TreeView Control   
Name: Nithya
Date: 10/11/2009 10:23:12 AM
Comment:
Thanks for the code. It saved my time. Happy Coding :)
Title: Its amazing   
Name: Yakub Pasha
Date: 10/10/2009 2:09:15 PM
Comment:
It’s really very good, Thanks a lot for the code .. this is exactly what I wanted.
Title: Low Performance   
Name: Ravikant
Date: 10/6/2009 10:44:27 AM
Comment:
I tried your code. But it takes lot time to show the TreeView.
Title: add a node at any leval   
Name: sam
Date: 10/5/2009 4:58:29 AM
Comment:
Hi can you give me any example of how to add a node at any leval please?
Title: hyperlink to the nodes   
Name: Ryan
Date: 9/21/2009 7:35:50 AM
Comment:
Hi anitha. what you could have is a link or path to a selcted item in the database. You can then add TreeView1.SelectedNode.Value. Then you should use the value that is returned to lookup the column in the database where the path to the specific file is. You can use the following code to get the data.

string databaseconnection = ConfigurationManager.ConnectionStrings["DatabaseName"].ConnectionString;
System.Data.SqlClient.SqlConnection myConnection = new System.Data.SqlClient.SqlConnection(databaseconnection);
System.Data.SqlClient.SqlCommand myCommand = new System.Data.SqlClient.SqlCommand("SELECT ColumnName FROM Table WHERE ID='" + valueFromTree +"'", myConnection);
myConnection.Open();
myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
while (myReader.Read())
{
Session["Item"] = Convert.ToString(myReader.GetString(0));
}
myReader.Close();
myConnection.Close();
Title: hyperlink to the nodes   
Name: anitha
Date: 9/19/2009 9:26:46 AM
Comment:
Hi ..
Thanks a lot for the code .. this is exactly what I wanted but in addition to this .. I would like the nodes to open a attachment when someone clicks on the name for Eg : when they click on Category 2.2.2 it should open a file from a folder .. the name of the file will be from database that is assigned to Category 2.2.2 ...... Can anyone Please help me ..... Please
Title: Need Help!   
Name: Gaurav
Date: 9/16/2009 11:33:16 AM
Comment:
Dear Sir,
I have one requirement. When user clicks on Clear button; i want to clear or change the selected node color to default using javascript so that he can asssume that the node is not selected. Please help me to get this.
Title: Excellent Code!!!!!!   
Name: Gaurav
Date: 9/16/2009 11:30:57 AM
Comment:
This code is awesome and unique in its kind.
Still very much practical.
Title: Have a hyperlink to a folder   
Name: Anitha
Date: 9/15/2009 11:05:52 AM
Comment:
is it possible to add another tables rows underneath each node ? like for eg: under Category 2.2.2 can I have a Repeater control to a column that opens a file ... Please help me .. Please
Title: Have a hyperlink to a folder   
Name: Anitha
Date: 9/15/2009 11:03:17 AM
Comment:
Hi ..
Thanks a lot for the code .. this is exactly what I wanted but in addition to this .. I would like the nodes to open a attachment when someone clicks on the name for Eg : when they click on Category 2.2.2 it should open a file from a folder .. the name of the file will be from database that is assigned to Category 2.2.2 ...... Can anyone Please help me ..... Please
Title: Display a popup window on a treeview node selection   
Name: tambi121
Date: 8/26/2009 7:02:38 AM
Comment:
Could you pl help in resolving the issue propping up when trying to display a pop up window upon selecting a node.
I have registered onselectednodeChanged client script event nd was calling javascript function to open a modaldialog window.It was working fine with the link button.Bt with Treeview nothing is opening except printing the method name on the browser.

Could you pl help me in figuring out this issue?

Thanks in advance
Title: help   
Name: noami
Date: 8/17/2009 11:35:51 PM
Comment:
Hi author:
I have a table called Category which only have 2 columns and don't have subcategories. How do i modify the codes above in order to make the tree view appeared as below:

Categories:
→ Ring
→ Anklet
→ Bangle
→ Brooch
→ Bracelet


My Category table is as below:

CategoryID CategoryName
1 Ring
2 Anklet
3 Bangle
4 Brooch
5 Bracelet
Title: Thanks for the compliments   
Name: Teemu
Date: 7/23/2009 6:38:17 AM
Comment:
Nice to know I've written something useful. :-)

ontreenodepopulate="TreeView1_TreeNodePopulate" is not needed in VB version since sample code has Handles clause there (it is equivalent), though it is matter of taste how one does wire up events.

C# version of the code download has this already.
Title: Great Stuff   
Name: Ryan
Date: 7/23/2009 6:08:05 AM
Comment:
Thanks. This is a great post. Should make a book out of it.

To make the nodes display. Add ontreenodepopulate="TreeView1_TreeNodePopulate" to the treeview that is created in the aspx file. This should solve a big problem
Title: THANKS   
Name: bassam
Date: 7/21/2009 4:19:30 AM
Comment:
I see the link to the sample code
when i restor database ti not restor
if you can help to get the other backup for database
to help me sent by
email:bass_57@hotmail.com
plssssssssssssssssss
ineed this code for multi node
Title: THANKS   
Name: Mark
Date: 7/10/2009 1:51:30 PM
Comment:
I see the link to the sample code has been fixed; thanks.
...and THANKS VERY MUCH for providing a VB version!

Proof of a great sample - look at how long ago you wrote this, and yet still so many people are finding it useful! Thanks again!
Title: Daud   
Name: Daud
Date: 7/9/2009 7:14:30 AM
Comment:
Great work
Title: Thank you   
Name: JYO
Date: 6/24/2009 8:28:50 AM
Comment:
very vERY Thanks
Title: Thank You   
Name: Vikas Vatnani
Date: 6/23/2009 2:49:20 AM
Comment:
I have the code you can mail me on vikasvatnani@gmail.com and i will fwd you the code file

Thanks
Title: "Download Sample Code" link broken?   
Name: Mark
Date: 6/22/2009 12:38:18 AM
Comment:
Hello - is this working for anybody?
Title: Solution to problem where ppl are unable to get the complete tree   
Name: Akanksha Negi
Date: 6/16/2009 5:02:40 PM
Comment:
Hi,
Though the reply is too late but may be it can of some help to ppl visiting this article now.

It's in reference to the problem like-

Title: I can't see neither child nodes nor plus icon
Name: Taleshi
Date: 4/3/2009 1:08:44 PM

/* CODE---*/

cmd.CommandText = "select categoryid,categoryname," +
"(select count(*) from category where parentid = category.categoryid) childnodecount " +
"from category where parentid IS NULL";

/* remaining CODE--*/

if you re-write it like-

cmd.CommandText = "select categoryid,categoryname," +
"(select count(*) from category where parentid = CATEG.categoryid) childnodecount " +
"from category as CATEG where parentid IS NULL";

it will work perfectly.
Title: Great Article   
Name: meikjohnson
Date: 6/11/2009 4:47:09 PM
Comment:
First time going through your article, building sql database table and implementing the code in C# worked great. Now I have to enable and disable imageurl's based upon access authentication.
thanks again.
Title: Data   
Name: Syed
Date: 6/11/2009 5:04:20 AM
Comment:
Can U send me the Data?. Still I Can't Understand the Data Structure
Title: Good and Best Concept!! Thankyou Sir   
Name: Ashish Sharma (Web Developer)
Date: 6/10/2009 1:12:37 AM
Comment:
thanks for providing the basic and best concept on treeview control. Thanks a lot sir.
Title: That's Ok   
Name: Patibandla
Date: 6/2/2009 9:10:40 AM
Comment:
Hi Teemu,

That's ok but if there are more Controls to be loaded then this will render the page slowly. So how to avoid that
Title: Great Stuff   
Name: V.R.Hari
Date: 5/28/2009 1:48:33 AM
Comment:
Excellent stuff. really cool. works fine for me.
thanks...
Title: Use Controls ASCX   
Name: Cas
Date: 5/24/2009 9:58:19 AM
Comment:
This is excellent code and it works well.

However I have been trying to make a WebUserControl of this - I usually load ASCX controls dynamically into an UpdatePanel on demand from a control outside of the UpdatePanel eg: using a choice from a listbox to populate an UpdatePanel (by first clearing it of the existing control and then adding the new control).

However I have been unsuccessful in trying to convert the treeview code in this article to an ASCX file that can be dynamically loaded on demand as descibed above - any ideas/examples ?
Title: Excellent   
Name: New tree view user
Date: 5/19/2009 5:36:22 AM
Comment:
Excellent
Title: Exception with treeview binding   
Name: Prabhu
Date: 5/7/2009 5:16:23 AM
Comment:
As i seened ur article its very usefull to me but at run time it is giving thet System.OverflowException:Arithmetic Overflow exception in counting pls give me the solution as early as possible
Title: Menu   
Name: Maran
Date: 5/4/2009 6:36:21 AM
Comment:
can it be applied for navigation menu whose menu items are soming from database?
Title: TreeView +CheckBoxs   
Name: mgdutt@gmail.com
Date: 4/22/2009 8:28:34 AM
Comment:
Hi

I am populated the data(3 levels) in treeview with Showcheckboxs property option set to true.I have checked the required fields in treeview.Now i want to read this treeview & insert data to database.

Please help me........
Thanks in Advance

Regards
Gurudutt
Title: I can't see neither child nodes nor plus icon   
Name: Taleshi
Date: 4/3/2009 1:08:44 PM
Comment:
Dear friends,
I can't see neither child nodes nor plus icon.althogh in my database there are parent nodes (Null parent) and child nodes, but just parent nodes exists without any plus.
please help me soon.
my email address : sd.talehi@gmail.com
my code is below :

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (! Page.IsPostBack)
{
PopulateRootLevel();
}
}

private void PopulateRootLevel()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=(local);Initial Catalog=eshop;Integrated Security=True";
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
cmd.CommandText = "select categoryid,categoryname," +
"(select count(*) from category where parentid = category.categoryid) childnodecount " +
"from category where parentid IS NULL";
SqlDataAdapter da = new SqlDataAdapter(cmd );
DataTable dt = new DataTable();
da.Fill(dt);
PopulateNodes(dt , TreeView1.Nodes );
}

private void PopulateNodes(DataTable dt,TreeNodeCollection nodes)
{
foreach(DataRow dr in dt.Rows) {
TreeNode tn= new TreeNode ();
tn.Text = dr ["categoryname"].ToString ();
tn.Value =dr ["categoryid"].ToString ();
nodes .Add (tn );
//tn.PopulateOnDemand = ((Int32 )(dr["childnodecount"]) > 0);

if ((Int32 )(dr["childnodecount"]) > 0)
Title: hi again   
Name: PoojaBindra
Date: 3/29/2009 3:27:38 AM
Comment:
Can anyone mail me the same program in vb.net plssssssssss
my mail id is poojabindra @in.com
Title: Its Really Great   
Name: PoojaBindra
Date: 3/25/2009 7:52:24 AM
Comment:
Plsss is there anyone ready to give me the same program in vb.net please its urgent
thanks in advance
Title: Thank You Very Much   
Name: Mrityunjay Ravi
Date: 3/16/2009 4:16:33 AM
Comment:
Hi,
It's a excellent programming. Thank you very much for helpping.
Title: Nice Article   
Name: Srinivasa Rao Alapati
Date: 3/14/2009 10:33:43 AM
Comment:
This article is very simple and working is very good
Title: Expand   
Name: Jim
Date: 2/25/2009 3:45:16 PM
Comment:
Thanks for the code but have one problem. when I set the Treeview's property ExpandDepth="0" then I can't expand the tree at all. When I remove that property, tree is populated with every nodes being expanded... I like to populate the tree with ExpandedDepth as 0. I did't modify your code at all.. any idea? thanks in advance
Title: hi - no performance on this code   
Name: moslem
Date: 2/1/2009 8:09:20 AM
Comment:
please contact me to order you a high level performance for this comment

moslem522@gmail.com
Title: Very Good   
Name: Niranj
Date: 1/30/2009 12:01:29 AM
Comment:
I realy thanks u for help me this site...
and i always use this site when i have problem
Title: Wonderful   
Name: Naushad
Date: 1/29/2009 2:06:34 AM
Comment:
Its helped me thanks
Title: treeview   
Name: Raghu
Date: 1/18/2009 2:37:41 AM
Comment:
Thanks for the good code. its working .but i have one small doubt " when i select one root node remaining nodes should be hide how can achive that?"
Title: regarding Orientation   
Name: manoj kunmar
Date: 1/11/2009 7:46:43 AM
Comment:
how i can chage orientation of a tree view as like that menu.....i want to show my data as binary tree
Title: I want The Root Level Nodes As Check Boxes   
Name: Sridhar Parshi
Date: 1/6/2009 8:17:48 AM
Comment:
i want to show the tree view like what you showned in above, but in the place of category1 2 3 i wnat show them as check boxes if any one selects that below child nodes are selected.
Title: Multi column Tree Controle   
Name: Noor Arshad
Date: 1/6/2009 2:59:23 AM
Comment:
Plz tell me how can i make it multicolumn?
Title: but   
Name: Jiya786
Date: 1/1/2009 1:18:04 AM
Comment:
hey ur code working smoothly but how can i prevent from postback on TreeNodePopulate event??????its again n again doing postback the page so any1 can help me plz ???give me feedback on funs_jiya@cybergal.com
Title: hi   
Name: Jiya
Date: 12/31/2008 9:21:30 AM
Comment:
onColaps i dont want postback how can i privent that post back any1 can help me plz
Title: Thanks   
Name: Jiya786
Date: 12/31/2008 7:48:34 AM
Comment:
ur solution was realy help me thanks a lot n plz go ahead god bless u
Title: Thanks   
Name: Fraser
Date: 12/17/2008 9:48:47 AM
Comment:
Hi - just a quick thanks for the code... very useful...

Fraser
Title: Showcheckbox   
Name: Danish
Date: 12/15/2008 9:32:21 AM
Comment:
hi
now im getting value of node whos hav checked=true u can see below lines of code
Dim userView As TreeView = form1.FindControl("TreeView1")
Dim strRights As String = ""
For Each nodes As TreeNode In userView.CheckedNodes
strRights = strRights & nodes.Value & ","
Next
TextBox1.Text = strRights
but now how can i save this value in my database.means which i checked=true.how can i update
anybody can help me out regarding this issue
regards,
Title: ShowCheckBox   
Name: Danish
Date: 12/14/2008 11:57:04 AM
Comment:
Hi,
How can activate ShowCheckBox with treeview and get the value as per checkbox value True.
Regards,
Title: Thank you so much   
Name: suresh Dhanapal
Date: 12/12/2008 9:18:32 AM
Comment:
Hi,
Thank you so much it's working super....
Title: This is much simpler   
Name: Darren Schulte
Date: 12/4/2008 5:48:24 PM
Comment:
1) Create your table that contains the tree data
e.g: ID,Folder key (string),Folder name (string),ParentKey(string)
2) Variables:Dim TNode As Node,Counter as Integer
3) Create first root node
With TreeArg
.Nodes.Clear
Set TNode = .Nodes.Add(, , "t_root", "Root")
end with
3) Load all records into the tree view control and make them children to root node
Do Until RS.EOF
Set TNode = .Nodes.Add("t_root", tvwChild, RS!FolderKey, RS!FolderName)
'Stick the parent node into the nodes tag property
TNode.Tag = RS!FolderParentKey
RS.MoveNext
Loop
4) And now...
For Counter = 1 To .Nodes.Count
If .Nodes(Counter).Text <> "Root" Then
Set .Nodes(Counter).Parent = .Nodes(.Nodes(Counter).Tag)
End If
Next Counter
Title: THANK YOU!   
Name: null talent
Date: 11/18/2008 1:07:46 AM
Comment:
Works like a charm! I can't thank you enough.
Title: Hey Its fantastic man, Thanks a lot   
Name: Naveen
Date: 11/17/2008 6:57:47 AM
Comment:
Hey man i was so much worried, but u made me so relaxed. Thanks a lot..
Title: Thanks   
Name: Mr Quach
Date: 11/17/2008 2:58:55 AM
Comment:
Thanks u.
I already fixd it. Cause from Oracle parameter
Title: Help me to fix error   
Name: Mr Quach
Date: 11/10/2008 12:35:51 AM
Comment:
I use code C# to oracle but it has an error
What can I fix it?

private void PopulateRootLevel()
{
OracleConnection objConn = new OracleConnection(@"Data Source=SALES;User Id=sa;Password=sa;");
OracleCommand objCommand = new OracleCommand(@"select id,title,(select count(*) FROM Category WHERE parentid=sc.id) childnodecount FROM Category sc where parentID IS NULL", objConn);
OracleDataAdapter da = new OracleDataAdapter(objCommand);
DataTable dt = new DataTable();
da.Fill(dt);
PopulateNodes(dt, TreeView1.Nodes);
}

private void PopulateSubLevel(Int32 parentid, TreeNode parentNode)
{
OracleConnection objConn = new OracleConnection(@"Data Source=SALES;User Id=sa;Password=sa;");
OracleCommand objCommand = new OracleCommand(@"select id,title,(select count(*) FROM Category WHERE parentid=sc.id) childnodecount FROM Category sc where parentID=@parentID", objConn);
objCommand.Parameters.Add("@parentID", OracleDbType.Int32).Value = parentid;
OracleDataAdapter da = new OracleDataAdapter(objCommand);
DataTable dt = new DataTable();
da.Fill(dt);
PopulateNodes(dt, parentNode.ChildNodes);
}


protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
PopulateSubLevel(Int32.Parse(e.Node.Value), e.Node);
}

private void PopulateNodes(DataTable dt, TreeNodeCollection nodes)
{
foreach (DataRow dr in dt.Rows)
{
TreeNode tn = new TreeNode();
tn.Text = dr["title"].ToString();
tn.Value = dr["id"].ToString();

nodes.Add(tn);

//If node has child nodes, then enable on-demand populating
tn.PopulateOnDemand = ((Int32)(dr["childnodecount"]) > 0); // Error at this line: Specified cast is not valid
}
}

What can I fix it?

Thanks!
Title: need 2 disable a few nodes...help!!!!!   
Name: anu
Date: 10/30/2008 9:11:24 AM
Comment:
great article .i tried it n it helped a lot for a newbie like me..but wat i need is 2 fill the tree with user roles..so some roles can only view certain child nodes..i tried 2 change the query to query by roles too but i belive it dont work that way..so i tot it would be better 2 load all nodes then iterate thru the nodes n disable the nodes which is not required for a certain role....is dat possible..can someone help me with this...

i guess il need the child node which i wanna disable den put it into a for loop right????........thanx in advance..
Title: Please help the last node keep expanding   
Name: Mohamed
Date: 10/29/2008 7:01:10 PM
Comment:
First, thank you very much for dharing the code.
I applied the code and it works to one exception my last node keep repeating. Here is my code and my SQL works when run outside it just retun one item but I run the example in my page it keeps adding the same node.

Thanks for your help.
Imports System.Data
Imports System.Data.SqlClient

Partial Class ManageCategories
Inherits System.Web.UI.Page


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
PopulateRootLevel()
End If
End Sub

Private Sub PopulateRootLevel()

Dim connectionString As String
connectionString = _
ConfigurationManager.ConnectionStrings("MyConnectionString").ConnectionString
Dim objConn As New SqlConnection
objConn.ConnectionString = connectionString
Dim objCommand As New SqlCommand("SELECT Menu_id as Id, Menu_Name as Name, " _
& "(SELECT COUNT(*) FROM vw_Category_Menu M where m.parent_id= vw_Category_Menu.Menu_id) childnodecount " _
& " FROM vw_Category_Menu Where parent_id is NULL", objConn)

Dim da As New SqlDataAdapter(objCommand)
Dim dt As New DataTable()
da.Fill(dt)

PopulateNodes(dt, TreeView1.Nodes)
End Sub


Private Sub PopulateNodes(ByVal dt As DataTable, _
ByVal nodes As TreeNodeCollection)
For Each dr As DataRow In dt.Rows
Dim tn As New TreeNode()
tn.Text = dr("Name").ToString()
tn.Value = dr("Id").ToString()
nodes.Add(tn)

'If node has child nodes, then enable on-demand populating
tn.PopulateOnDemand = (CInt(dr("childnodecount")) > 0)
Next
End Sub
Private Sub PopulateSubLevel(ByVal parentid As Integer, _
ByVal parentNode As TreeNode)
Dim connectionString As String
connectionString = _
Title: Its working   
Name: atuda2000
Date: 10/22/2008 1:29:29 AM
Comment:
Hi author.

thanks a lot. the treeview just work nicely. actually it had me write different SQL query to retrieve data from my database. but with just that, i can make good treeview.

i still not exploring the depth anyway. which im gonna do now.

thanks, author. good job.
very helpful.

atuda2000
Title: Child node   
Name: Vishal
Date: 10/14/2008 8:52:22 AM
Comment:
Hi
This code is working fine.But when i click on last node means there no any child node i want to fetch data related to that node value id from database how i will do it.

Give me reply ASAP
Title: Treeview PopulateOnDemand does not show updated results   
Name: Sat
Date: 10/2/2008 3:28:12 PM
Comment:
Hi,
When I expand the node for the first time it executes the PopulateOnDemand method, but then on it is not executing that command. Now the problem is it is showing stale data(when someone changes the info listed in the treeview)
Title: Display Hierarchical Data with TreeView in ASP.NET 2.0   
Name: sai vara prasada rao
Date: 9/20/2008 7:38:08 AM
Comment:
hi ,i have applied the code of tree view in my application but it is not displaying the child nodes when i click on the + (plus sign) it is not at all show the sub node .

plz kindly solve my problem . im posting my code , plz any body solve it and mail it my mail id im providing



using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Messages : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["strcon"]);
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
PopulateRootLevel();

}
}
protected void TreeView1_TreeNodeCollapsed(object sender, TreeNodeEventArgs e)
{
}
private void PopulateRootLevel()
{
SqlCommand cmdscrap = new SqlCommand("select senrolno,SenderName,sbatch, scenter, Semail, Ssubject,SBodytxt,Renrolno, Rname, RBatch, RCenter, REmail, Rsubject, Rbodytxt, DateMsgSend,(select count(*) FROM ibsaf_scrap WHERE senrolno=sc.senrolno) childnodecount FROM ibsaf_scrap sc where senrolno IS not NULL", con);
SqlDataAdapter da = new SqlDataAdapter(cmdscrap);
DataTable datable = new DataTable();
da.Fill(datable);
PopulateNodes(datable, TreeView1.Nodes);
}
private void PopulateNodes(DataTable dt, TreeNodeCollection nodes)
{
foreach (DataRow dr in dt.Rows)
{
TreeNode tn = new TreeNode();
tn.Text = dr["sendername"].ToString();
//tn.Value = dr["senrolno"].ToString();
nodes.Add(tn);

//If node has child nodes, then enable on-demand populating
tn.PopulateOnDemand = ((int)dr["childnodecount"] > 0);


my mail id is
saivaraprasadarao@gmail.com
Title: it is perfect   
Name: Erol Çağlar - Aygaz IT
Date: 9/15/2008 8:20:09 AM
Comment:
it is running to Oracle.
Title: VERY GOOD   
Name: Mohammad javed Comm-IT India P Ltd.
Date: 9/10/2008 3:06:15 AM
Comment:
Excellent articles..
Title: Help me   
Name: Min
Date: 9/9/2008 4:56:46 AM
Comment:
Help me ~ I've got the error on my sqlcommand

@"select id,title,(select count(*) FROM SampleCategories sc WHERE parentid=sc.id) childnodecount FROM SampleCategories sc where parentID IS NULL",objConn)

da.Fill(dt); <--Error message

anybody help ~~ its made by c#
Title: excellent   
Name: sanjeev
Date: 8/29/2008 5:30:28 AM
Comment:
it is very helpfull to me
thanks
sanjeev
Title: Perfect   
Name: gaurav mehta
Date: 8/19/2008 2:20:59 AM
Comment:
nothing could better then this
Title: This was great   
Name: Paul
Date: 8/14/2008 10:39:34 AM
Comment:
Fantastic. This works great. I needed to use tree view to list reports in datawarehouse by report type and this fits the bill perfectly.
Title: Thanks   
Name: A.Ibrahim
Date: 8/14/2008 10:12:33 AM
Comment:
Thank you very much Teemu . it's work out
Title: Excellent, gj !!!   
Name: Lancelot.Net
Date: 8/11/2008 11:13:49 AM
Comment:
Simple, easy to understand. Great article! Ty :)
Title: Excellent! !!!!!!!!!!!!   
Name: Srikanta
Date: 8/8/2008 5:42:41 AM
Comment:
This is just grt article
Title: Just Wow Grt !!!   
Name: Swati
Date: 8/5/2008 6:18:45 AM
Comment:
This is just grt article. It helped me to clear my concepts...
Title: Excellent!   
Name: Zee
Date: 8/1/2008 7:34:05 AM
Comment:
After weeks of looking for a good, or even close TreeView example. This worked out great for me. Thanks.
Title: ExpandDepth = 0   
Name: Inv
Date: 7/30/2008 10:05:01 AM
Comment:
Thanks! The most useful part was setting ExpandDepth="0" to TreeView, otherwise, very very weird behavior occured..
Title: Clicking item does not post back   
Name: sangam
Date: 7/29/2008 8:24:41 AM
Comment:
It worked fine for me. Thanks. But clicking any of the item does not lead to post back. What is the reason?

Regards
Sangam
http://dotnetspidor.blogspot.com/
Title: Data Entry   
Name: Goose
Date: 7/23/2008 9:54:26 PM
Comment:
I was able to get the treeview to populate correctly. My goal is to incorporate other data with the treeview. A data entry like form in treeview mode. Any ideas?
Thanks.
Title: Perfect   
Name: Bart
Date: 7/22/2008 5:26:19 AM
Comment:
Hello Teemu Keiski,
This is just what I need.

Tks
Best regards
Bart
Title: Javascript in TreeNodePopulate   
Name: olyvia
Date: 7/22/2008 4:27:57 AM
Comment:
I have a populateondemand treeview. In the TreeNodePopulate event, which is fired on clicking the nodes, I want to put a javascript function. I cannot use RegisterStartUpScript because the event does not post back. Is there any way I can put a javascript function over there?
Title: devx   
Name: Panos
Date: 7/17/2008 5:46:29 AM
Comment:
Thank you so much.
Exactly what I was looking for.
Title: nice   
Name: chan myie
Date: 7/17/2008 4:07:13 AM
Comment:
thank u so much that is the thing i need
Title: cool code   
Name: gundevar
Date: 7/16/2008 2:00:37 PM
Comment:
the c# side of the code works with only little tweaking of a local connection string. I did not change anything else.
No instance failure for C# code.
Thanks a lot Teemu, I've been struggling with this for 2 days.
All the best!
Title: Code   
Name: Teemu
Date: 7/11/2008 3:41:20 AM
Comment:
Both VB.NET and C# code were equally tested when I wrote the article and worked 100% in the scenario I used as basis. If you get such failures, your connection string is wrong, you refer wrong objects with different IDs etc. Remember, it cannot match 100% your setup, at least db connection string must be changed.
Title: An error Occured in The C# Code   
Name: Rei Ayanamu
Date: 7/10/2008 11:46:19 PM
Comment:
The c# code of this program is experiencing instance failure....
Title: C# Sample code   
Name: Teemu
Date: 7/8/2008 11:09:11 AM
Comment:
Use the [download sample code] link on top of the page. You'll find the code in C# and VB.NET...
Title: wonderful   
Name: sartaj
Date: 7/8/2008 11:05:25 AM
Comment:
BUT I WANT THE CODE IN C#
Title: TREEVIEW   
Name: SEJAL
Date: 7/2/2008 5:31:49 AM
Comment:
Very nice example....it is very useful...keep going...
all the best....
Title: keep state   
Name: Hi
Date: 6/27/2008 5:51:23 AM
Comment:
How to keep the treeview state after tn.NavigateUrl
Title: Simple and Clear example   
Name: Mallikarjun
Date: 6/25/2008 2:23:27 AM
Comment:
Simple and Clear example and works fine
Title: TreeView   
Name: Mark
Date: 6/9/2008 5:19:22 PM
Comment:
Hey, Jim thanks for the advertisement! But I'm going with something that is free, thanks. Good example by the way, Teemu!
Title: tree   
Name: sesha
Date: 6/9/2008 1:37:46 AM
Comment:
usefull
Title: TreeView   
Name: chandar
Date: 6/3/2008 5:25:31 AM
Comment:
Hi Teemu,

It is Fantastic and very good Examples.I appreciate u to give more idea working with that control
Title: Multiple Columns   
Name: Jim
Date: 5/28/2008 6:06:22 PM
Comment:
Here is one that has multiple columns. It still has on-demand populating, postback events and hyper links too.

http://www.DigitalTools.com/GVT.aspx
Title: Gridview Data Entry   
Name: hutty
Date: 5/26/2008 9:36:17 AM
Comment:
Great article. It works for me, which is phase 1. Now I need to incorporate treeview into a gridview data entry form. I had found the perfect article, but PC crashed and I can't find it again. Thanks.
Title: Top to Bottom display   
Name: dosdemon
Date: 5/14/2008 3:30:17 AM
Comment:
how can we display treeview from top- to bottom rather then from left to right.
Title: Displaying data with TreeView   
Name: Ryan
Date: 5/7/2008 6:33:12 AM
Comment:
This is a really good example - Im ashamed to say that I didnt even attempt to code it myself before trawling the net for a solution, but with a couple of tweaks to use Generics for the data (as I do) this worked first time!

Well done Teemu Keiski on such a well written example!

PS. (for earlier poster) there is c# code in the download example..
Title: Displaying Specific data with TreeView   
Name: Cheyne
Date: 5/5/2008 7:26:38 AM
Comment:
Hi,

This code works, but displays ALL of the data on the treeview. But Id like SPECIFIC data to show, depending on who logs into the system.... how do I do that ?
Title: Issue while using TreeNodecollapsed or TreeNodeExpanded events   
Name: Unlimited
Date: 4/30/2008 2:14:17 AM
Comment:
I used the code to implement the dynamic treeview and it worked fine.
But when i generated the TreenodeCollapsed and TreenodeExpanded event, then on initial page load it works fine but when i clicks to expand a subfolder it calls treenodeCollapsed first before page load.

Below is my code :


Option Explicit On
Option Strict On

Imports System.Web
Imports System.Data
Imports CHV_ITC_GilFileDetail_1_BL
Imports CHV_ITC_GILFileDetail_1_DLR
Imports CHV_ITC_GilFileDetail_1_DLW
Imports CHV_ITC_GILFileDetail_1_EH

Partial Class TreeView
Inherits System.Web.UI.Page

'Global variables
Dim dsFolder As DataSet
Dim dsSubFolder As DataSet
Dim dtGetOverrideDetails As DataTable
Dim dtResetOverrideDetails As DataTable
Dim objDLR As CHV_ITC_GILFileDetail_1_DLR.DataRead = Nothing
Dim objDLW As CHV_ITC_GilFileDetail_1_DLW.DataWrite = Nothing
Dim objBLR As CHV_ITC_GilFileDetail_1_BL.BusinessLayer = Nothing
Dim folderFlag As Integer = 0
Dim dr As DataRow

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
PopulateRootLevel()
txtParentIDs.Attributes.CssStyle.Add("display", "none")
End If
End Sub

Private Sub PopulateRootLevel()
objDLR = New CHV_ITC_GILFileDetail_1_DLR.DataRead
dsFolder = objDLR.get_GSPFolderTest("0")
PopulateNodes(dsFolder.Tables(0), TreeView1.Nodes)
End Sub

Private Sub PopulateNodes(ByVal dt As DataTable, ByVal nodes As TreeNodeCollection)
For Each dr In dt.Rows
Dim tn As New TreeNode()
tn.Text = dr("LASTFOLDER").ToString()
tn.Value = dr("FOLDER_SEQ").ToString()

tn.NavigateUrl = "#"
tn.SelectAction = TreeNodeSelectAction.Select
nodes.Add(tn)

'If node has child nodes, then enable on-demand populating
tn.PopulateOnDeman
Title: Doing the same thing by calling a store prodedure   
Name: chame
Date: 4/28/2008 1:06:16 PM
Comment:
Hello,

I am trying to do the same thing but with ASP, there is a store procedure that's we have to do something similar that assign item from the "title" column for example a level number.

So I'm trying to do the same thing but when I call the function for this store procedure, it's display all my data as parent nodes instead of sorting it in the proper order. Do you have any suggestion of how I can get this to function properly since I am calling a stored procedure, and working with ASP?

Thanks in advance!!
Title: keeeka   
Name: suribabu
Date: 4/23/2008 3:11:59 AM
Comment:
Dis really helped me a lot Thanq very very much
Title: Good Stuff   
Name: Jeremy
Date: 4/17/2008 5:34:36 PM
Comment:
Hey works for me! Thanks!
Title: Kalyan1982   
Name: Kalyan
Date: 4/17/2008 8:15:39 AM
Comment:
Hi,

In the above example, If I click on Category 2.1.1 on this event

Protected Sub TreeView1_SelectedNodeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.SelectedNodeChanged

End Sub

I need to get the whole path which is

Category2/Category 2.1/Category 2.1.1

Please help!
Title: It already is in C#   
Name: Teemu
Date: 4/16/2008 7:15:52 AM
Comment:
See the code download (the link is bit small on top of the article, but it is there :-) ): http://authors.aspalliance.com/joteke/treeviewv2article.zip
Title: .cs file   
Name: tariq
Date: 4/16/2008 5:35:57 AM
Comment:
nice example.i m bigginer pls. can u post same in C# coding.
Title: Thanks a lot...Good example   
Name: anitha
Date: 4/8/2008 9:22:35 AM
Comment:
Example is good
Title: Nice Eplaination   
Name: Nikhil V.Thakre
Date: 4/4/2008 2:21:55 AM
Comment:
Explanation of treeview is very good .........
Title: Great Example   
Name: Raghava.G.N
Date: 4/3/2008 5:08:43 AM
Comment:
Example was excellent
by
Raghava.G.N
raghavagn1@rediffmail.com
Title: Thanks Alot   
Name: Kishor Patil
Date: 4/2/2008 11:09:10 AM
Comment:
Thank's For This Code....
I Need It Too Much...
Title: Thanks   
Name: Serkan SÖNMEZ
Date: 3/27/2008 7:07:54 AM
Comment:
that was great,
Thank you Teemu Keiski
Title: Great article   
Name: Juan Fco Roman Ochoa
Date: 3/12/2008 6:10:56 PM
Comment:
Thnaks man , this article is going to be very usefull for me to develop a project given to me, thanks a lot
Title: Great!!   
Name: Jon
Date: 3/11/2008 9:38:20 PM
Comment:
Great Article...

I seriously don't understand why anyone would use VB, when C# is so much better.
Title: very useful article   
Name: Anushka
Date: 3/11/2008 4:51:55 AM
Comment:
Such a nice article....
but can u plz tell me how to change color of a selected node...
Title: Josh   
Name: dilshad
Date: 3/10/2008 8:07:04 AM
Comment:
Thanks....
Title: bindind TreeView with datagrid   
Name: Nishant Singh
Date: 3/5/2008 12:35:49 AM
Comment:
Teemu,
that was great.
can u throw some light on binding treeview control with a datagrid
Title: Superb Work   
Name: Srinivasan
Date: 2/28/2008 11:27:04 PM
Comment:
t tried the same concept for more than two concepts, but after seeing r code, i got the idea and implemented it...thank u so much.HATS OFF to ur work...
Title: Such a Nice Code   
Name: Jay Mehta
Date: 2/20/2008 1:57:54 PM
Comment:
Such a Nice code that helps me lot. Do you have this type of code for menu control or is it same for menu control?

Thank you
Title: Node refresh   
Name: AP
Date: 2/20/2008 1:39:33 PM
Comment:
Thank you for posting this - it is extremely helpful!

I was wondering how would I be able to extend this further so if I were to add/delete a node in the database via a webform, the parent node of the TreeView would automatically refresh?
Title: Excellent work! - Light Waight   
Name: Venkat Rao Konka
Date: 2/16/2008 12:14:46 AM
Comment:
Nice and simple Snippet. Right usage of Tree View Control. Hope s this gives dare to tree view by programmers. All the Best.
Thank You
Venkat Konka
Title: Please help!   
Name: Vansana
Date: 2/7/2008 2:28:53 PM
Comment:
Hi all, Greate work!. I got question similiar to the last post. How do I populate another sub under each Category? Say if I have products assiciated with earch category. Hope this making sense.

Thanks inadvance

Vansana
Title: Excellent work!   
Name: Jara
Date: 2/7/2008 3:30:11 AM
Comment:
This is very nice work!!! but how do I associate the categories or subcategories with products to make it like real world stuff? For example if I have CategoryID link to Products table as foreing key.

Thank you very much.
Jara
Title: very good   
Name: Ezhil
Date: 2/5/2008 7:15:30 AM
Comment:
Hi, i searched lot for the tree view, It is the best stuff
thanks a lot
Title: Nice post   
Name: kalyan
Date: 2/4/2008 4:13:25 PM
Comment:
Your post solved my problem.
Title: Very good stuff   
Name: marty
Date: 2/3/2008 4:59:48 AM
Comment:
i am a newbee and i search a long time and here i found the right stuff very good and thank you :-)
Title: Great   
Name: dammyTosh
Date: 1/24/2008 5:22:43 PM
Comment:
Men u are great it simply solved my 1 week head ache.
Title: See docs   
Name: Teemu
Date: 1/24/2008 1:44:24 AM
Comment:
From: http://technet.microsoft.com/en-us/library/e8z5184w.aspx

Using the TreeView Control with UpdatePanel Controls
UpdatePanel controls are used to update selected regions of a page instead of updating the whole page with a postback. The TreeView control can be used inside an UpdatePanel control with the following restrictions:

TreeView callbacks must be associated with asynchronous postbacks, or callback event validation will fail. When you set the PopulateOnDemand property of the TreeNode controls to true, callbacks are enabled. You can use one of the following approaches to ensure that TreeView callbacks work with the UpdatePanel control:

If the TreeView control is not inside an UpdatePanel control, disable callbacks on TreeNode controls that are not part of an asynchronous postback. To do this, set the PopulateOnDemand property to false.

Programmatically refresh all controls that register callbacks during the asynchronous postback. For example, you can place the TreeView control inside an UpdatePanel control. The TreeView control does not have to be in the UpdatePanel control where the asynchronous postback originates, as long as the UpdatePanel control that contains the TreeView control is refreshed.

You must apply styles by using a reference to a Cascading Style Sheet (CSS) class. For example, instead of setting the NodeStyle property by using a property-subproperty attribute, set the style by using the property-CssClass attribute. Similarly, when you use the NodeStyle template to set the style, use the CssClass attribute of the template.

The EnableClientScript property must be true (the default value). In addition, if callbacks are enabled for the TreeView control, you cannot change the EnableClientScript property between asynchronous postbacks.
Title: Integrate with Ajax Issue   
Name: Hardy Wang
Date: 1/23/2008 8:32:47 PM
Comment:
I tried this solution, it works perfectly. Then I tried to embed treeview into a ajax updatepanel, now I comes with some problem

I expand one node, then select some other node (select not expand), by this time TreeNodePopulate and SelectedNodeChanged both will be fired, and then keep clicking on this single node (already selected), TreeNodePopulate envent will be fired again and again which causes sub nodes of first expanded node keep grouping and duplicating.

If I remove updatepanel to use regualar postback mode, I don't have this issue.

Any idea?
Title: I Need Some Other Help   
Name: subhranshu
Date: 1/23/2008 12:19:01 AM
Comment:
How can i show the system data in treeview control...
suppose i want to show all c:/ documents..that means c:/ should be the parent one and rest sub-folders should come in chlid like and so on.. and please give the code in c#
Title: I Need Some Other Help   
Name: Subhranshu
Date: 1/23/2008 12:17:03 AM
Comment:
How can i show the system data in treeview control...
suppose i want to show all c:/ documents..that means c:/ should be the parent one and rest sub-folders should come in chlid like and so on..
Title: V Good   
Name: Carl
Date: 1/10/2008 5:19:55 PM
Comment:
I was up and running within 2 minutes. Thanks for the excalant example. simple and easy to follow.
Title: Fine   
Name: Rajkumar
Date: 1/4/2008 12:08:37 AM
Comment:
i want to display the data on same window by clicking tree node.so how is possible..can i know that type of code...
Title: its fine,but i want to explorer type treeview to display the data on the same window by clicking on treenodes   
Name: anu
Date: 12/28/2007 12:02:25 AM
Comment:
its fine,but i want to explorer type treeview to display the data on the same window by clicking on treenodes
Title: Nods are not accessable, after populating with the help of a button event   
Name: Chandrajit Samanta
Date: 12/20/2007 2:09:31 AM
Comment:
I am using ASP.NET 2.0 Treeview control. During initial page load treeview is not populated. Onclick of a button treeview is papulated. But populating I am trying to access all those nodes from codebehind, using another button click event in the same page. But in the second button click event treeviev control is showing empty, in codebehing, where as the page is still displaying all those populated node. Please tell me how to access all those node from codebehind...

Chandrajit Samanta
chandrajit.samanta@gmail.com
9986044300, Bangalore , India
Title: Works but need another help   
Name: Rey
Date: 12/18/2007 12:38:15 PM
Comment:
As I said, I'm new to Expression Web and ASP (though I know VB6 and VFP, and I already implement Treeview in different way).

I was able to avoid re-displaying the treeview everytime clicking node by adding this code in the populatenodes, just after nodes.Add(tn)

tn.NavigateUrl = "javascript:void(0)"

Now, my problem was when you select another node, the previous node change the color to like disabled (its look like revisited link).

My another problem is to get the value of the selected node as I would like to pass the value to open a page browser to display information of the selected node...

Looking forward to any help and link that I could resolve my problem.

Thanks again for the great code!

Rey
Title: It works fine but I need another help   
Name: Rey
Date: 12/18/2007 2:39:13 AM
Comment:
First of all, thank you so much for the sample code. It help me to save time. I am very new to web thing.

Can you please extend your help to me for 2 things that I need to implement.

1. Can the treeview be populated before the page display? Same as the other comment below?

2. In node click event, I need to pass the ID# (not the newid()) so I can open a browser page that contains the information of the node click.

Thanks you in advance.
Title: Node not a member of system.args   
Name: jip
Date: 12/11/2007 10:50:56 PM
Comment:
inline " PopulateSubLevel(CInt(e.Node.Value), e.Node) "
it gives me an error message 'Node is not a member of System.Args' can any one him me please....
Title: Wonderful   
Name: Your friend
Date: 12/10/2007 7:07:52 AM
Comment:
Thanks................. Greate job
Title: Thanx   
Name: Siju George
Date: 12/6/2007 3:30:18 AM
Comment:
Thanx so much for this help

www.codepal.co.in
Title: Nested Gridview footer total by using javascript   
Name: ram
Date: 12/5/2007 1:34:08 AM
Comment:
I want to display Nested Gridview footer total by using javascript .i am 2 gridviews(gvEmployee(Empcode,sal -fields)(gvDepartment(Deptcode,sal-2fields).I want to display both totals in separate .sal is template field.
its v.v.urgent.

thnx
Title: Excellent Example - Need some enhancement   
Name: Cherry
Date: 12/3/2007 12:16:24 PM
Comment:
Above example is working fine. Can you let me know if i want, How to add root node (category 1, category 2, ...) statistically(i.e., not from the database table) along with dynamic (from dataset) parent & child node population?
Title: Sample Code   
Name: Teemu
Date: 12/3/2007 7:35:02 AM
Comment:
See the "Download Sample Code" on top side of the page. There's both C# and VB.NEt code.
Title: C#   
Name: Sujith
Date: 12/3/2007 7:31:19 AM
Comment:
Can you Give me the code for the same in C# asp.net
Title: Great Job!   
Name: Tim
Date: 11/29/2007 1:50:24 PM
Comment:
Great article. I was up and running within 5 minutes. Thanks for being succinct and supplying the code examples.
Title: Anyone get 'Object Expected' error ?   
Name: jake
Date: 11/21/2007 1:11:40 PM
Comment:
this works great, but I can't expand/collapse as the generated javascript causes an 'object expected' error.. anyone have a solution to this ?
Title: Too good for developers   
Name: Chetan Nagar
Date: 11/16/2007 12:51:01 AM
Comment:
This is best implementation of treeview using codes.
Title: Greate work   
Name: Mem
Date: 11/11/2007 11:12:39 AM
Comment:
Well done! simple and efficient.
now i have to figure how to retain the selected node state
10X
Title: Thanks, It's a good code :)   
Name: Roberto Londono
Date: 11/7/2007 4:13:35 PM
Comment:
Hey Teemu, Very nice code, It work how exsample in my application very well. If you need reference.
Roberto Londono Echeverry
Washington State University
Education and Public Media
rlondono@wsu.edu
Title: Thanks   
Name: Susant
Date: 11/7/2007 5:28:19 AM
Comment:
Hello..

Your article Help me
Title: Where is   
Name: Teemu
Date: 11/5/2007 4:36:06 AM
Comment:
2.1 is title, not parentid. Otherwise, I don't understand what you're referring to. The code sample above contains tested code, which is emphasized on the article examples, and it works just fine with no overflows.
Title: Thanks, It's a good code :)   
Name: Yebay
Date: 11/5/2007 2:17:46 AM
Comment:
This one is a good code, heheheh

but if the parentid is an integer type with the value like 2.1, 2.1.1, 2.1.1.1 it will return 2 to parentid and caused stackoverflow exception, oohohoho so change it to string will solve a problem.
Title: Postback   
Name: Teemu
Date: 11/2/2007 9:31:27 AM
Comment:
As I write in the article: "Note that clicking on the node does cause a postback because I haven’t modified the select action of the populated tree nodes from the defaults.
"
Title: A Problem in you treeview   
Name: Ashrafali
Date: 11/2/2007 9:01:29 AM
Comment:
Hello Brother,
Thankyou for your solution. It is nice and very helpful to me. But I face one problem. When I click on node's text it got postback and load same data once more.

So please check it and guide me proper solution

thanking you
Ashrafali
Title: On Page Load all the Nodes should be expanded ?   
Name: Sehul Soni
Date: 10/30/2007 10:51:56 AM
Comment:
Hi,

This is nice example.
Thanks a lot. It helped me lot.
Is it possible that at Page Load automatically all the Nodes expanded ?
One more bug i found is when I am clicking on Sub Node it is creating whole tree again and again.

help me ?

Reply on sehulsoni@gmail.com

Thanks
Sehul
Title: Perform ALL roundtrips on page load first?   
Name: wizkidd
Date: 10/29/2007 12:24:25 AM
Comment:
Is it possible to alter the code so that the tree is entirley populated before at page load? This way we wouldnt have to invoke OnTreeNodeExpanded="TreeView1_TreeNodePopulate" ?? If this was possible, it would eliminate the annoying postback which I find the only poor thing.

Anyone wanna give eliminating the postback a shot?

- WizzKidd
Title: Great Program   
Name: Amit Jha
Date: 10/26/2007 8:35:49 AM
Comment:
Really this program helped me. I am so oblige to you.
Title: TreeView   
Name: CK
Date: 10/5/2007 4:04:31 PM
Comment:
Great Article for displaying data!

(1)Is there a way to display the childnode data onclick of it's parent?

(2) Then I insert all the user's choices in a related table using the following fields.

Projid Type Description

100 Goal PI-1 Blah blah goal choice 1

100 Objective PI-1.1 Blah Blah objective a

100 Goal TI-3 Blah Blah goal choice

100 Objective TI-3.1 Blah Blah objective a

100 Objective TI-3.2 Blah Blah objective b

100 Objective TI-3.3 Blah Blah objective c


After the user submits there data I give a web page which displays everything they entered. I show a Treeview which displays only the Goals and Objectives they selected, but it lists ALL the objectives for each goal. I would like it to show only the objectives with its master goal.

Any help is appreciated! Thank you.
Title: OnTreeNodeExpanded="TreeView1_TreeNodePopulate"   
Name: D.Bopp
Date: 9/28/2007 10:55:47 AM
Comment:
Good piece of code, thanks.

I had to add OnTreeNodeExpanded="TreeView1_TreeNodePopulate" to the TreeView to get it run properly. Took me a while to find that bug.
Title: Tree View   
Name: Adarsh Tated
Date: 9/27/2007 3:04:21 AM
Comment:
Hi I am Adarsh I am Not Able To understand how to populate data from sql server on tree view this Article give me a full support Thanks To Lot
Title: refresh   
Name: dany
Date: 9/25/2007 7:22:10 PM
Comment:
how can i refresh the tree when i updatae my db
the problem is when i use the functuion PopulateRootLevel()
i get another tree view
Title: Awesome Article   
Name: SM
Date: 9/5/2007 9:14:44 AM
Comment:
...this helped me a lot!
Title: help me in implementing tree view for MS CRM   
Name: Suresh Chinna
Date: 9/4/2007 6:27:33 AM
Comment:
Hi friends,

Can anyone suggest me how to implement treeview control in our aspx page( which is extension of MS CRM application) with the datas across different tables, where datas being stored in MS CRM Database

help me out

PS; give some sample codes for implementing
Title: Please help me!   
Name: Sriram
Date: 8/25/2007 4:54:29 AM
Comment:
Can you suggest me a way of getting the check boxes which are checked in the treeview using the above sample as reference?
Title: Gr8 Article!!!   
Name: Sriram
Date: 8/25/2007 4:52:36 AM
Comment:
This was a great article!!!Thanks a lot! I have a similar requirement and was struggling to get this done!This perfectly works!
Title: Doesn't work in Firefox in my case   
Name: Dimitar Kyuchukov
Date: 8/22/2007 6:06:30 AM
Comment:
The menu works perfectly well in IE but not in Firefox/1.5.0.12. Still Your article is really helpful.
Title: Good One,very Usefull   
Name: Anish V S
Date: 8/18/2007 3:56:32 AM
Comment:
Hi Thanks for your code
Title: Its Really GOOOOOoooooooooooooOOOOOOOD   
Name: ViREnDRa KumAr KohlI
Date: 8/10/2007 4:52:00 AM
Comment:
Hi,
Its Really GOOOOOoooooooooooooOOOOOOOD
Code,
Outstanding.................
Very Helpfull to mEeeeeee
Title: New Node   
Name: Claf
Date: 8/8/2007 10:45:02 AM
Comment:
Who insert/delete/update nodes?
Title: Nice One   
Name: Santosh
Date: 7/31/2007 8:41:17 AM
Comment:
nice one .it help me a lot

thanks
Title: Tnx Alot   
Name: Lior Cohen
Date: 7/28/2007 11:42:04 PM
Comment:
I was strongeld with the treeview for days!
this code is perfect , no other words! you trolly help me out!
Title: Tree Node   
Name: Shuchi
Date: 7/19/2007 1:21:29 AM
Comment:
It is Very Good Example

Thanks
Title: TREE nODE   
Name: Rahul P
Date: 7/5/2007 7:39:23 AM
Comment:
Perfect Example
Thanks
Title: I get duplicate data on the tree   
Name: Allan
Date: 7/3/2007 7:46:29 PM
Comment:
Hey, thanks for the tutorial, I tried running your code with a stored proc as the sql command argument. In my SQL management studio, I get the correct number of returns. On the tree view, I get exactly 2 sets of them. Where could I have gone astray. I didn't make any other mods except replacing your inline sql with a sproc.
Title: Works good - Save State of Tree?   
Name: James
Date: 6/28/2007 3:01:55 PM
Comment:
This worked out for me. Is their a way to save the state of the tree when you are dynamically populating the treeview, with ought having to iterate through all the nodes? Or can someone point me to a good article that explains how to do this? Thanks

answer to: diablo64@gmail.com
Title: Error if the child node is clicked.   
Name: jossy
Date: 6/28/2007 2:24:47 AM
Comment:
HI its a good code. I am able to populate the tree but if i click the node one more time null point error is comming.
Title: this does not work   
Name: Ritesh
Date: 6/25/2007 9:00:19 AM
Comment:
i had tried the same tutorial but when i click on other controls it still populates tree with same child node again & again what can be the solution...
Title: Image   
Name: Teemu
Date: 6/20/2007 12:04:58 PM
Comment:
Shouldn't setting image for a TreeNode be as simple as setting the ImageUrl property to point to the image? You can also set specifically ExpandImageUrl, CollapseImageUrl, NoExpandImageUrl (though these three are given at the TreeView)
Title: if use image to create organization chart .Is it possible?   
Name: lermpon
Date: 6/20/2007 3:39:44 AM
Comment:
if use image to create organization chart .Is it possible?
Title: Toggle Root/Parent Nodes Text   
Name: Adolph Key
Date: 5/26/2007 7:38:48 AM
Comment:
Hi Teemu, great code. I wanted to know how can I get the text for the Root and Parent Nodes to Toggle just like the expand(+)/collapse(-) images?
Title: Fill Hierarchichal data in tree view   
Name: Leela Pradeep Reddy
Date: 5/13/2007 3:53:37 AM
Comment:
Hi it's Very nice Example for Tree view. Thank you very much
Title: Fill Hierarchichal data in tree view   
Name: Puneet Khurana
Date: 5/10/2007 5:12:18 AM
Comment:
it is very nice example of populating a asp.net treeview with hierarchichal data.
Title: Any Code in vb.net   
Name: Riyas Thangal P S
Date: 4/25/2007 7:13:23 AM
Comment:
Hi Any code for displaying hierarchial data in vb.net please send me to riyasthangal@yahoo.com...Any help would be greatly appreciated. thanks
Title: Selected   
Name: phlipdawg
Date: 2/19/2007 10:39:40 AM
Comment:
Hi, I am trying to set a flag when I select a node. I've tried using the Selected property, but it always comes back as "false" I'm trying to use one of the noads as a logout

any help would be greatly appreciated. thanks.
Title: tree view   
Name: Rakesh
Date: 2/15/2007 2:08:58 AM
Comment:
there i have used the teree view in asp.net2003 but new i am converting this project .net 2005 so it gives th error in following.........
System.Web.UI.WebControls.TreeNode tnParent=new System.Web.UI.WebControls.TreeNode();
//
tnParent.Type="P";//error
tnParent.ID=dr/error[0].ToString();
tnParent.Text=dr[2].ToString();
tncNode.Add(tnParent);



how can i remove this
pls mail me on rakes.j@panteq.com
Title: add remove node   
Name: fariba
Date: 2/10/2007 5:57:58 AM
Comment:
how can add romove a node and refresh to show new nodes or remove nodes in tree.
Title: Thanks   
Name: Alex
Date: 2/9/2007 2:44:15 PM
Comment:
Great code, up and running in no time.
Title: NavigateUrl   
Name: Teemu
Date: 2/4/2007 1:46:06 AM
Comment:
NavigateUrl is property of TreeNode, just like Text and Value are. My sample already sets Text and Value, so adding NavigateUrl to thje process is very trivial (one more property to set)
Title: treenode   
Name: phlipdawg
Date: 2/2/2007 2:50:27 PM
Comment:
or is there a way to use the DB to assign the url?
Title: treenode   
Name: phlipdawg
Date: 2/2/2007 2:49:11 PM
Comment:
Could you post a short example, my apologies, I'm very new to this
Title: URL   
Name: Teemu
Date: 2/2/2007 12:27:35 PM
Comment:
Set the NavigateUrl property of a TreeNode. It puts the node in navigation mode (when clicked you're redirected to the URL).
Title: Link Nodes to a url?   
Name: phlipdawg
Date: 2/2/2007 10:20:18 AM
Comment:
This is great, but how do you link the nodes to URL's?
Title: Use Code Download   
Name: Teemu
Date: 1/31/2007 9:09:48 AM
Comment:
The "Download Sample Code" link at top of the article might help. :-)

There's the source both in VB and C#.
Title: Convert code to C#   
Name: zia Ul Hassan
Date: 1/31/2007 5:38:40 AM
Comment:
I am trying to convert this code to C#. I got stuck at this point
Handles TreeView1.TreeNodePopulate

any idea?
Title: Treeview Control   
Name: Henning
Date: 1/27/2007 6:46:41 AM
Comment:
Thanks,
I saved a lot of time with this codesample :-)
Title: Treeview controls   
Name: Charlie R
Date: 1/14/2007 10:14:40 PM
Comment:
Many thanks Teemu! I had tried many other ways of doing this and was getting tired and then your code worked the very first time. Thanks again, Charlie
Title: Excellent   
Name: Irfan
Date: 12/31/2006 5:34:44 AM
Comment:
Very Excellent !! Keep it up
Title: TGridView in TreeNode   
Name: Teemu
Date: 12/6/2006 11:05:48 AM
Comment:
Yes, it is. You can use techniques demonstrated in following blog post to render a grid in TreeView: http://weblogs.asp.net/dannychen/archive/2006/01/25/436454.aspx

If you use a GridView as instantiated control to get grid-like output, there can be certain challenge with state management side. You probably need to create custom TreeView as well to deal with that side. However, just rendering a grid/table with given HtmlTextWriter within TreeNode should be easy task.
Title: Treeview controls   
Name: ravi
Date: 12/6/2006 9:49:05 AM
Comment:
Truly great stuff.

Is it possible to bind a gridview in treeview?
Title: Link problem   
Name: Stig Barnney
Date: 12/4/2006 4:05:54 AM
Comment:
Hi, nice code and well explained.

I also have this problem, when I link each SubCategory to an aspx page, the tree closes back to root.

Thanks

Barnney
stig@barnney.dk
Title: Treeview Controls   
Name: Adityan
Date: 11/13/2006 1:56:22 AM
Comment:
very useful! Great article..
Title: Treeview controls   
Name: Ranu
Date: 11/7/2006 6:34:51 AM
Comment:
Hi,
Code may be useful ,but i tried to run this code n its not working.The query which u have used is not clear.

select id,title,(select count(*) FROM SampleCategories " _
& "WHERE parentid=sc.id) childnodecount FROM SampleCategories sc where parentID=@parentID
Title: Tanks   
Name: Bulent Cavan
Date: 11/6/2006 12:18:59 PM
Comment:
Thanks, :)
Title: Nice code   
Name: Marko
Date: 11/3/2006 9:38:12 AM
Comment:
Hi, nice code and well explained many thanks.

I have the same problem as a post earlier but could see a response.

Basically

when I link each SubCategory to an aspx page, the tree closes back to root. Any help would be welcome

Thanks
Marko
Title: Treeview controls   
Name: Cem Aksoy
Date: 10/27/2006 10:50:51 AM
Comment:
Impressive, it's quite explicit and useful.Thanks for your great article.In the meanwhile Adnan, dont look up for hairy,possible you are Turkish and you understood me ;)
Title: Treeview controls   
Name: Alex
Date: 10/20/2006 10:41:11 AM
Comment:
Thank you, I try it and its help me. But, if I open page vith TreeView1.visible=false, and on Button.Click I make TreeView.visible-true subcategory dosn't open. Some body know why?
Thanks
Title: Great Code   
Name: Sebastian
Date: 10/18/2006 11:43:50 PM
Comment:
Hi, it's great!!!! But I have a problem and it's when I link each SubCategory to an aspx different, the tree goes back and it appears closed from the root... I don't know what to do.... please Heeeelp!!!! Sorry for my english I'm from Argentina, and I don't use writting in english... :)
Pls, answer to seba_la_x@yahoo.com
Title: Thanks   
Name: John
Date: 10/15/2006 10:23:41 PM
Comment:
This is awesome. Thanks!
Title: Treeview controls   
Name: Joey
Date: 10/10/2006 6:26:24 PM
Comment:
Thank you so much for this! I wish there were lots of good information out like this on the Internet! :)
Title: Treeview Controls   
Name: Joyce and Renuka
Date: 10/6/2006 5:35:09 AM
Comment:
Good stuff!!!!!
We have a tree view which has a multilevel hierarchy
Project-A
Estimate-A
Area-A
Area-B
System 1
System 2
Product 1

with least database trips how can we get this hierarchy?
Title: Tree View Control   
Name: Mohammed Ghouse Barq
Date: 9/27/2006 1:22:29 AM
Comment:
This is excellent, I really like approach.
Title: waooooo   
Name: Abdulla Hussein
Date: 9/25/2006 3:36:33 PM
Comment:
Thank you, I try it and its help me
Title: Treeview Controls   
Name: Uday
Date: 9/21/2006 5:11:26 AM
Comment:
Thanks very much for this simple and precise article that explains the concept to the point. Awesome!!
Title: Re: Treeview controls   
Name: Teemu
Date: 9/4/2006 3:04:25 PM
Comment:
Lata,

TreeView in ASP.NET is in System.Web.UI.WebControls namespace (System.Web.dll assembly)
Title: Re: Treeview   
Name: Teemu
Date: 9/4/2006 3:03:03 PM
Comment:
Adnan, you need first think does representing data in TreeView make sense. Basically if you have 3 or 4 tables and it's direct parent-child relationship, idea is eseentially the same as with self-referencing table (and yes, it can make sense). First get the root level, then query for the next and so on. Using separate tables basically makes the levels in the tree fixed, as well as queries getting specific levels.
Title: Re: Treeview Control   
Name: Teemu
Date: 9/4/2006 3:00:06 PM
Comment:
sc is just shortcut to SampleCategories. See the "FROM SampleCategories sc" in where clause. Essentially it's using a subquery to get count of child nodes for a parent node and sc is used to reference the parent table from the subquery.
Title: Treeview Control   
Name: Ren
Date: 9/4/2006 1:48:12 PM
Comment:
I tried the code and it worked, thanks. I have used basic query strings before, but nothing like this. What does sc.id represent? Also, what is this "sc" between SampleCategories and WHERE for?

select id,title,(select count(*) FROM SampleCategories " _
& "WHERE parentid=sc.id) childnodecount FROM SampleCategories sc where parentID IS NULL

Thanks,

-Ren
Title: Treeview   
Name: Adnan
Date: 8/29/2006 8:56:26 AM
Comment:
Let suppose if i have many (3-4) tables in SQL server 2005 linking with each other, and i want to disply all records in treeview what i should i do ?
Title: Treeview Controls   
Name: Mali
Date: 8/27/2006 4:56:30 PM
Comment:
This is a perfect reference for the dynamic treeview population. Thanks for everyone who contributes to this code.
Title: Treeview controls   
Name: Lata
Date: 8/22/2006 5:32:13 PM
Comment:
Good one!! I m getting an error though "Treeview doesnt exist in the current context". Am I missing any namespace? Also is there a way to have datagrid itself in the child node?
Title: Too many round-trips   
Name: Deepak Aggarwal
Date: 8/17/2006 7:41:38 AM
Comment:
Hi
Truly a great stuff.
However, 'n ' database calls will be made where n is the number of nodes having child element. If 'n' is too big, this application will consume a lot of memory. Cant you retrive the entire structure in one go, save it in xml and manage on client side? please suggest code for this.
Title: Treeview controls   
Name: Tim
Date: 8/3/2006 6:23:18 PM
Comment:
Brilliant!! Thank you so much for a nicely written and explained solution. After a few changes to field names and database connection strings, it worked with the table I have already populated with data.
Title: Treeview controls   
Name: Sandeep
Date: 7/21/2006 1:51:29 AM
Comment:
This is one of the nice code..........
Title: Treeview Controls   
Name: Paul
Date: 7/14/2006 2:55:28 PM
Comment:
Thank you very much. I just started with ASP.NET and I've been looking for days for a way to populate the treeview from a DB. The problem was always that the code was way over my head and they didn't explain it very well. More like "Here's the code. Enjoy". You took the time to explain all the steps and I'll be able to use this example with my application.
Title: Treeview controls   
Name: Rincy
Date: 7/10/2006 7:22:05 AM
Comment:
This coding was very useful.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2014 ASPAlliance.com  |  Page Processed at 4/19/2014 8:37:52 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search