TreeView Control unexpected event behavior

Posted by ProgNet on Stack Overflow See other posts from Stack Overflow or by ProgNet
Published on 2010-04-16T22:19:27Z Indexed on 2010/04/16 22:23 UTC
Read the original article Hit count: 227

Filed under:
|
|
|

Hi all,

In the MSDN is writen about TreeNode that:

"By default, a node is in selection mode."

"To put a node into selection mode, set the node's NavigateUrl property to an empty string."

"When a node is in selection mode, use the SelectAction property to specify which event or events are raised when a node is selected."

"Setting TreeNodeSelectAction value TreeNodeSelectAction.Select Raises the SelectedNodeChanged event when a node is selected."

Please see TreeNode

Here is the problem and possibly a bug in the control:

When I set the TreeNode object PopulateOnDemand value to true and call the Collapse() function on that node. Then the TreeNodeExpanded event is raised in addition to the SelectedNodeChanged event. This is in complate contradiction to what is writen in the MSDN. According to the MSDN this sould happen only if TreeNodeSelectAction Property is set to TreeNodeSelectAction.SelectExpand value.

Does some know whats the cause for that?

Here is the code:

<asp:TreeView ID="TreeView1" runat="server" AutoGenerateDataBindings="False" 
                    onselectednodechanged="TreeView1_SelectedNodeChanged" 
                    ontreenodepopulate="TreeView1_TreeNodePopulate">
                </asp:TreeView>



protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        string path = Server.MapPath(".");
        PopulateTopNodes(path);

    }
}

//MSDN : Occurs when a node with its PopulateOnDemand property set to true is expanded in   //the  TreeView control.
protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
   LoadChildNode(e.Node);      
}

private void PopulateTopNodes(string pathToRootFolder)
{
    DirectoryInfo dirInfo = new DirectoryInfo(pathToRootFolder);
    DirectoryInfo[] dirs = dirInfo.GetDirectories();
    foreach (DirectoryInfo dir in dirs)
    {
        string relativePath = (dir.FullName).Replace(pathToRootFolderPrefix, "");
        TreeNode folderNode = new TreeNode(dir.Name, relativePath);

        if (dir.GetDirectories().Length > 0)
        {
            folderNode.PopulateOnDemand = true;
            folderNode.Collapse();

        }
        folderNode.NavigateUrl = "";
        folderNode.SelectAction = TreeNodeSelectAction.SelectExpand;
        TreeView1.Nodes.Add(folderNode);
    }
}

 private void LoadChildNode(TreeNode treeNode)
{


    string d = treeNode.NavigateUrl;
    string action = treeNode.SelectAction.ToString(); 


    string fullPath = Path.Combine(pathToRootFolderPrefix, treeNode.Value);
    DirectoryInfo dirInfo = new DirectoryInfo(fullPath);


    DirectoryInfo[] dirs = dirInfo.GetDirectories();
    foreach (DirectoryInfo dir in dirs)
    {


        string relativePath = (dir.FullName).Replace(pathToRootFolderPrefix, "");
        TreeNode folderNode = new TreeNode(dir.Name, relativePath);

        if(dir.GetDirectories().Length>0){
            folderNode.PopulateOnDemand = true;
            folderNode.Collapse();

        }
        folderNode.NavigateUrl = "";
        folderNode.SelectAction = TreeNodeSelectAction.SelectExpand;
        treeNode.ChildNodes.Add(folderNode);
    }
}

//MSDN:Occurs when a node is selected in the TreeView control.
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{


}

Thanks

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about treeview