views:

235

answers:

3

This is in reference to yesterday's question "How do I create folders in ASP.NET in code behind". The problem is that I want to create dynamic folders at run time. Folder names will be entered via a TextBox and output will be displayed in a TreeView. The form will submit if I enter the first folder name into textbox1 and click the "Add Folder" button. When I submit multiple folders with the same name, the output should be an indexed increment of the name (e.g., FooFolder, FooFolder(2), FooFolder(3), etc). There are two events: Add Folder Event and Remove Folder Event. If I select a particular child folder and click on the "Remove folder" button, the folder will be removed. For adding a folder I have written the following code:

TreeNode tnode = new TreeNode();
if (TreeView1.Nodes.Count > 0)
        {
            int found = 0;
            for (int i = 0; i < TreeView1.Nodes.Count; i++)
            {
                if (TreeView1.Nodes[i].Text == TextBox1.Text)
                    found += 1+i;
            }
            if (found > 0)
            {
                tnode.Text = TextBox1.Text + found.ToString();
            }
            else
            {
                tnode.Text = TextBox1.Text;
            }
        }
        else
        {
            tnode.Text = TextBox1.Text;
        }
        TreeView1.Nodes.Add(tnode);
}

In my code, the ChildNode index is not incrementing; it is always 1, like this:

Sumit
Sumit(1)
Sumit(1)
Sumit(1)
Amit
Amit(5)
Amit(5)
Amit(5)

In the treeview, I have set ImageSet="XPFileExplorer". So the output should look like this:

-Root
        -Sumit(Parent1)
                  NewFolder
                  NewFolder(2)
                  NewFolder(3)
                  NewFolder(4)
                  NewFolder(5)
         -Amit(Parent2)
                  FooFolder
                  FooFolder(2)
                  FooFolder(3)
                  FooFolder(4)
                  FooFolder(5)

If I delete any child folder, say, Newfolder(3) and Newfolder(4) and create these same folders under the same Sumit(Parent1), the index should be Newfolder(3),Newfolder(4). If I create one more NewFolder under Sumit with same name then the index should be NewFolder(6).

Could somebody please modify my code to get this desired output?

+1  A: 

Hi. Before you do this, I learned the hard way that you should not create/remove folders under a running application, or you will cause your app pool to recycle. So make sure that you are creating directories somewhere else on the server. (Hopefully you have that access)

NetHawk
+1  A: 

Your issue here is your algorithim to detect if the item exists. Basically your code:

for (int i = 0; i < TreeView1.Nodes.Count; i++)            
{                
    if (TreeView1.Nodes[i].Text == TextBox1.Text)
                    found += 1+i;            
}
if (found > 0)            
{                
   tnode.Text = TextBox1.Text + found.ToString();      
}            
else            
{                
   tnode.Text = TextBox1.Text;            
}

Let's walk through this. The user submits NewFolder your code goes through and doesn't find any node called NewFolder, so it sets the node to NewFolder.

Now the user clicks add again for NewFolder, this time it finds NewFolder so the new name becomes NewFolder1.

Now the user clicks add again for NewFolder, this time it finds NewFolder so the new name becomes NewFolder1.

Your comparing if TreeView1.Nodes[i].Text == TextBox1.Text, which only one node will ever have this name. You will need to strip off the numeric portion of the name.

If your using a naming convention like NewFolder(1) then you can easily do this. But based on the code you have there the name of the node would be NewFolder1

JoshBerke
+1  A: 

Your text comparison is off. Since you may have added numbers to previous nodes under the same parent, you will only encounter the new name once.

It should look like:

if (TreeView1.Nodes[i].Text.StartsWith(TextBox1.Text))
    found++
SirDemon
this code is not giving desired output.If i enter Jack as folder name 6 times the series is coming.Jack,Jack(1),jack(3),Jack(6),jack(10),jack(15) which is absolutely wrong output.
I edited to fix your found count as well.
SirDemon
hi in your coding subfolder index is starting from 1 where as it should have started with 2 ex.If Jack is Parent folder Michael are subfolders it should be under Jack Michael,Michael(2),Michael(3),Michael(4)
And if i add one more Parent folder say Simon n its subfolders are NewFolder,NewFolder(2),NewFolder(3),NewFolder(4),Newfolder(5)..And again i select the parent folder Jack and entered Michael as subfolder then this michael should come under Jack Michael(5).
but its index is coming Michael(6) after Newfolder(5).
You're supposed to only count nodes inside the parent where you're creating a subfolder. The simple index will also not solve other problems in your code, since we don't have all your code, nor do we have the exact logic you want. To get it right, you STILL have to do it yourself.
SirDemon