Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
string[] tagdata = {"TestAlarm1.T1", "Test1.S1.S4", "TestAlarm1.T2", "test2.T1", "Test1.S1.S2.Tag1"}

i have above data, i had done the same in windows application before which worked.
but in ASP.net i m not able to use "ContainsKey" property.
How can i solve the same in ASP??
TIA

What I have tried:

oreach (string name in data)
                    {
                        if (nodes.ContainsKey(name))
                        {
                            node = nodes[name];
                        }
                        else
                        {
                            node = new TreeNode(name);
                            node.Name = name;
                            nodes.Add(node);
                        }
                        nodes = node.Nodes;
                    }

this worked in windows application but not working in ASP.net
Posted
Updated 3-Oct-19 22:53pm

1 solution

In general, you cannot copy'n'paste some Windows Forms code into an ASP.NET project and expect it to work, because objects are not the same between both technologies.
A Windows Forms TreeView is not the same as an ASP.NET TreeView, even if they have the same name.

ASP.NET TreeView has a FindNode(string)[^] which returns a TreeNode if specified string is found, and otherwise returns null.
This would give:
C#
foreach (string name in data)
{
   if ((node = treeView.FindNode(name)) == null)
   {
      // code to add a new node here
   }
   nodes = node.ChildNodes;
}

Note that an ASP.NET TreeNode does not have a Nodes property; instead it has a ChildNodes property.
You should always refer to the core documentation of the objects you intend to use:
TreeView Class (ASP.NET)[^]
TreeNode Class (ASP.NET)[^]
 
Share this answer
 
Comments
Member 12694392 4-Oct-19 7:45am    
TreeNodeCollection nodes = TreeView1.Nodes;
foreach (string name in data)
{
if ((node = TreeView1.FindNode(name)) == null)
{
node = new TreeNode(name);
nodes.Add(node);
}
nodes = node.ChildNodes;
}
done but not working correctly in this case.
NoBug.T1.S1
NoBug.T1.S2
NoBug.T1.S3
NoBug.T2.N1
NoBug.T2.N2
NoBug.T2.N3
T1 and T2 appears multiple times which is not right.
phil.o 4-Oct-19 8:18am    
Not having access to your solution nor the actual values in nodes while your application is running, I'm afraid I would have quite a hard time guessing what the issue could be.
You will have to put a breakpoint at the beginning of the method, press F5, and start debugging line-by-line from here. Examine carefully the values in the treeview's nodes. Do you need help on how to conduct a debug session in Visual Studio?
Member 12694392 6-Oct-19 23:14pm    
in the above example NoBug is parent and T1 is parent1 but when i plot the tree w.r.t. above example i get multiple T1.
the logic mentioned only compares parent not parent1.
TIA
BillWoodruff 5-Oct-19 0:31am    
+5

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900