开发者

TreeView Control unexpected event behavior

开发者 https://www.devze.com 2022-12-27 09:46 出处:网络
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.\"

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.Select;
        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.Select;
        treeNode.ChildNodes.Add(folderNode);
    }
}

//MSDN:Occurs when a node is selected in the TreeView control.
protected void TreeView1_SelectedNodeChanged(object sender, EventArg开发者_如何学Gos e)
{


}

Thanks


I'm don't know ASP.Net, but just reading your code you are setting it to SelectExpand but in your comments above it sounds like you think you're setting it to Select (or this just a typo in the sample code?). In both methods in your sample it's written as:

folderNode.SelectAction = TreeNodeSelectAction.SelectExpand;
0

精彩评论

暂无评论...
验证码 换一张
取 消