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?