Click here to Skip to main content
11,409,181 members (58,389 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET Treeview
- 1
     1.1
     1.2
   - 1.3
         1.3.1
         1.3.2
        - 1.3.3
            1.3.3.1
            1.3.3.2
+ 2
+ 3
Hi All,

For example if we consider that the above mentioned is like Treeview,Here total 5 parent nodes are there those are
1,
1.3,
1.3.3,
2,
3

For Parent Nodes Like,

1
1.3
1.3.3

I wrote 3 for each loop’s if you observe in my code.
protected void tvCategories_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
    {
        try
        {
            foreach (TreeNode treenode in tvCategories.Nodes)
            {
                if (treenode.Text == e.Node.Text)
                {
                    treenode.Expand();
                    treenode.ImageUrl = "~/Images/OpenedBookColorInBlue.png";
                    break;
                }
                foreach (TreeNode t in treenode.ChildNodes)
                {
                    if (t.Text == e.Node.Text)
                    {
                        t.Expand();
                        t.ImageUrl = "~/Images/OpenedBookColorInBlue.png";
                        break;
                    }
                    foreach (TreeNode t1 in t.ChildNodes)
                    {
                        if (t1.Text == e.Node.Text)
                        {
                            t1.Expand();
                            t1.ImageUrl = "~/Images/OpenedBookColorInBlue.png";
                            break;
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.Write(ex.ToString());
        }
    }
 
protected void tvCategories_TreeNodeCollapsed(object sender, TreeNodeEventArgs e)
    {
        try
        {
            foreach (TreeNode treenode in tvCategories.Nodes)
            {
                if (treenode.Text == e.Node.Text)
                {
                    treenode.Collapse();
                    treenode.ImageUrl = "~/Images/ClosedBookColorInBlue.png";
                    break;
                }
                foreach (TreeNode t in treenode.ChildNodes)
                {
                    if (t.Text == e.Node.Text)
                    {
                        t.Collapse();
                        t.ImageUrl = "~/Images/ClosedBookColorInBlue.png";
                        break;
                    }
                    foreach (TreeNode t1 in t.ChildNodes)
                    {
                        if (t1.Text == e.Node.Text)
                        {
                            t1.Collapse();
                            t1.ImageUrl = "~/Images/ClosedBookColorInBlue.png";
                            break;
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.Write(ex.ToString());
        }
    }

Now For Example If my Treeview is like below mentioned,How many forech loops i want to write??

- 1
 
    1.1
 
    1.2
 
   - 1.3
 
           1.3.1
 
           1.3.2
 
         - 1.3.3
 
                1.3.3.1
 
                1.3.3.2
 
                - 1.3.3.3
 
                       1.3.3.3.1
 
                       1.3.3.3.2
 
                       - 1.3.3.3.3
 
                              1.3.3.3.3.1
+ 2
 
+ 3

According to my code for the above mention treeview I want to write 5 for each loop’s
Suppose If i have n-parent nodes what can i do????
If i increase my code like that it will be bad & it gets too much lenthy code

so it is not correct naa.

So what was the solution for this???

Please give me...

Thanks in Advance,
VenkiDesai.k
Posted 8-Nov-12 2:33am
Edited 8-Nov-12 18:34pm
v4
Comments
manognya kota at 8-Nov-12 8:45am
   
How are you populating your tree view?Is it from database or
dynamic ?
Venki Desai at 9-Nov-12 0:11am
   
From Database

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

hi,

this link will help you.

Loading a treeview to n levels[^]
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 8,676
1 OriginalGriff 6,885
2 Maciej Los 3,322
3 Abhinav S 3,238
4 Peter Leow 3,034


Advertise | Privacy | Mobile
Web03 | 2.8.150414.5 | Last Updated 9 Nov 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100