Asp TreeView OnTreeNodeExpanded事件阻止树视图扩展

时间:2015-01-13 20:37:42

标签: c# asp.net treeview

在Web表单上使用asp:TreeView控件并在后面的代码中填充节点时,它会按预期展开和折叠。但是,当我添加OnTreeNodeExpanded事件时,它会导致回发,但从未向此事件触发事件,并阻止节点扩展。我试图保存会话中扩展的节点,并在用户重新访问页面时恢复树视图扩展状态。任何想法如何用这种方法来实现这一目标。 谢谢!

以下是代码段:

<asp:TreeView ID="tvDevices" runat="server"  OnTreeNodeExpanded="TvDevices_OnTreeNodeExpandedCollapsed"     OnTreeNodeCollapsed="TvDevices_OnTreeNodeExpandedCollapsed"/>


protected void Page_Load(object sender, EventArgs e)
    {
        foreach (string s in devices)
        {
            TreeNode tn = new TreeNode();
            tn.Text = s.toString();
            tn.SelectAction = TreeNodeSelectAction.None;
            tvDevices.Ndes.Add(tn);
        }
        if (IsPostBack)
            {
                if (Session["tvDevicesState"] == null)
                    tvDevices.CollapseAll(); //Default to collapsed state
                else
                {
                    RestoreTreeViewState(tvDevices.Nodes, Session["tvDevicesState"] as List<string>);
                }
            }
            else
            {
                tvDevices.CollapseAll();
            }

    }
private void SaveTreeViewState(TreeNodeCollection treeNodes, List<string> expandedList)
    {
        foreach (TreeNode treeNode in treeNodes)
        {
            if (treeNode.ChildNodes.Count > 0)
            {
                if (treeNode.Expanded.HasValue && treeNode.Expanded == true)
                {
                    expandedList.Add(treeNode.Text);
                }
                SaveTreeViewState(treeNode.ChildNodes, expandedList);
            }   
        }            
    }

    private void RestoreTreeViewState(TreeNodeCollection treeNodes, List<string> expandedList)
    {            
        foreach (TreeNode treeNode in treeNodes)
        {
            treeNode.Expanded = false;
            if (expandedList.Contains(treeNode.Text))
            {
                if (treeNode.ChildNodes.Count > 0)
                {                         
                    treeNode.Expanded = true;
                    RestoreTreeViewState(treeNode.ChildNodes, expandedList);
                }                 
            }                
        }
    }

    protected void TvDevices_OnTreeNodeExpandedCollapsed(object sender, EventArgs e)
    {                     
        List<string> expandedList = new List<string>();  

        SaveTreeViewState(tvDevices.Nodes, expandedList);
        Session["tvDevicesState"] = expandedList;
    }   

2 个答案:

答案 0 :(得分:1)

我找出了为什么没有触发OnTreeNodeExpanded事件的原因。在我的情况下,原因是树节点没有附加值属性。向每个节点添加值后,事件被触发。 以下是工作代码。

<asp:TreeView ID="tvDevices" runat="server"  OnTreeNodeExpanded="TvDevices_OnTreeNodeExpandedCollapsed"     OnTreeNodeCollapsed="TvDevices_OnTreeNodeExpandedCollapsed"/>

代码背后:

 protected void Page_Load(object sender, EventArgs e)
{

        List<TreeNode> root = new List<TreeNode>();
        for(int i = 0; i < 5; i++)
        {
            root.Add(new TreeNode("Parent Node " + i, i.ToString()));
        }
        foreach (TreeNode tn in root)
        {
            tn.ChildNodes.Add(new TreeNode("First Child", "first"));
            tn.ChildNodes.Add(new TreeNode("Second Child", "second"));                              
            tvDevices.Nodes.Add(tn);
        }
        tvDevices.CollapseAll(); //Collapse all nodes
        RestoreTreeViewState(tvDevices.Nodes, (List<string>)Session["tvState"] ?? new List<string>()); //Restore previously expanded nodes

}
private void SaveTreeViewState(TreeNodeCollection treeNodes, List<string> expandedList)
{
    foreach (TreeNode treeNode in treeNodes)
    {
        if (treeNode.ChildNodes.Count > 0)
        {
            if (treeNode.Expanded.HasValue && treeNode.Expanded == true)
            {
                expandedList.Add(treeNode.Value);
            }
            SaveTreeViewState(treeNode.ChildNodes, expandedList);
        }   
    }            
}

private void RestoreTreeViewState(TreeNodeCollection treeNodes, List<string> expandedList)
{            
    foreach (TreeNode treeNode in treeNodes)
    {

         if(expandedList.Count == 0)
            return;
        if (expandedList.Contains(treeNode.Value))
        {
            if (treeNode.ChildNodes.Count > 0)
            {                         
                treeNode.Expand();
                RestoreTreeViewState(treeNode.ChildNodes, expandedList);
            }                 
        }                
    }
}

protected void TvDevices_OnTreeNodeExpandedCollapsed(object sender, EventArgs e)
{                     
    List<string> expandedList = new List<string>();  
    SaveTreeViewState(tvDevices.Nodes, expandedList);
    Session["tvDevicesState"] = expandedList;
}   

答案 1 :(得分:0)

由于您为treeview的Expanded / Collapsed事件添加了事件处理程序,因此它现在会触发回发。在调用树视图的事件处理程序之前,将调用Page_Load事件处理程序。在Page_Load事件处理程序中,在对树视图控件执行操作之前先检查Page.IsPostBack,例如默认情况下将其折叠。