Validated Treeview nodes do not return in order

I am using TreeView with ShowCheckBoxes = "All" in an ASP.NET 3.5 web application and for some reason the checked nodes are not returned in order. Suppose I have nodes A, B, C, and I select B and C and click the save button, and when I check the CheckedNodes TreeView property, the checked nodes are in order (B, C). But next time, when I return to the page and select node A, order B, C, A is returned. What could be the reason for this behavior?

+3
source share
2 answers

CheckedNodes is a TreeNodeCollection that only implements ICollection. When the checkChanged event occurs, it probably just adds tree nodes to the CheckedNodes collection.

Nothing to be seen in MSDN implies that you should assume that the nodes will be ordered. All he says is:

Each time a page is sent to the server, the CheckedNodes collection is automatically populated with selected nodes.

From your experiment, it seems safe to assume that in the second postback, it simply adds new checked nodes to the collection, instead of clearing the collection and re-adding everything.

+2
source

CheckedNodes. (http://urenjoy.blogspot.com/2009/06/sort-checkednodes-treenodecollection-in.html), , , . .

:

  private TreeNodeCollection SortTreeNode(TreeNodeCollection nodeList)
    {

       for (int i = 0; i < nodeList.Count-1; i++)
        {
            for (int j = i + 1; j < nodeList.Count; j++)
            {
                if (nodeList[i].Text.CompareTo(nodeList[j].Text)>0)
                {
                    TreeNode temp = nodeList[i];
                    nodeList.RemoveAt(i);
                    nodeList.AddAt(i,nodeList[j-1]);
                    nodeList.RemoveAt(j);  
                    nodeList.AddAt(j, temp);
                }

            }
        }
        return nodeList; 
    }

:

var tncInOrder = SortTreeNode(this.MyTreeView.CheckedNodes);

foreach (TreeNode node in tncInOrder )
{
  //Iterate through the nodes in order
}
0

Source: https://habr.com/ru/post/1727162/


All Articles