• TreeView状态保存


    很多时候会看到,使用Treeview的时候展开这个节点,到另外一个页面的时候Treeview的状态又恢复了,下面就是解决方法咯

    新建一个类

    public class TreeViewState
    {
        
    public void SaveTreeView(TreeView treeView, string key)
        
    {
            List
    <bool?> list = new List<bool?>();
            SaveTreeViewExpandedState(treeView.Nodes, list);
            HttpContext.Current.Session[key
    + treeView.ID] = list;
        }


        
    private void SaveTreeViewExpandedState(TreeNodeCollection nodes, List<bool?> list)
        
    {
            
    foreach (TreeNode node in nodes)
            
    {
                list.Add(node.Expanded);
                
    if (node.ChildNodes.Count > 0)
                
    {
                    SaveTreeViewExpandedState(node.ChildNodes, list);
                }

            }

        }


        
    private int RestoreTreeViewIndex;

        
    public void RestoreTreeView(TreeView treeView, string key)
        
    {
            RestoreTreeViewIndex
    = 0;
            RestoreTreeViewExpandedState(treeView.Nodes,
                (List
    <bool?>)HttpContext.Current.Session[key + treeView.ID] ?? new List<bool?>());
        }


        
    private void RestoreTreeViewExpandedState(TreeNodeCollection nodes, List<bool?> list)
        
    {
            
    foreach (TreeNode node in nodes)
            
    {
                
    if (RestoreTreeViewIndex >= list.Count) return;

                node.Expanded
    = list[RestoreTreeViewIndex++];
                
    if (node.ChildNodes.Count > 0)
                
    {
                    RestoreTreeViewExpandedState(node.ChildNodes, list);
                }

            }

        }

    }

    在页面代码,TreeView的控件里事件里写

        protected void TreeViewMain_Unload(object sender, EventArgs e)
        
    {
            
    // save the state of all nodes.
            new TreeViewState().SaveTreeView(TreeViewMain, this.GetType().ToString());
        }
  • 相关阅读:
    form表单为什么不能提交
    遇到了消息堆积,但是问题不大
    面试题:如何保证消息不丢失?处理重复消息?消息有序性?消息堆积处理?
    Dubbo学习地址
    Dubbo入门到实战2
    Dubbo入门到实战
    Mybatis 的三种执行器
    从源码理解Druid连接池原理
    Getting NoSuchMethodError:javax.servlet.ServletContext.getVirtualServerName()
    解决问题:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
  • 原文地址:https://www.cnblogs.com/javabin/p/1583003.html
Copyright © 2020-2023  润新知