Reputation: 85
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?
Upvotes: 0
Views: 841
Reputation: 1758
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++
Upvotes: 1
Reputation: 67108
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
Upvotes: 2
Reputation: 1412
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)
Upvotes: 1