简体   繁体   中英

Maintain the state of collasped/Expanded node in treeview control in asp.net

I have created a user control of the treeview. I need to maintain his state in the postback of the page. To maintain the state of the treeview I have used the session, however I have used the out proc session management and due to this treeview session object cannot be serialised. Is there any other way to maintain the state of the treeview. Here is the code

  <asp:TreeView Height="100%" ID="MyTreeView" OnTreeNodeCollapsed="MyTreeView_TreeNodeCollapsed" OnTreeNodeExpanded="MyTreeView_TreeNodeExpanded"  />


  protected void MyTreeView_TreeNodeCollapsed(object sender, EventArgs e)
    {
        if (!IsPostBack) return;
        state = new TreeViewState("TreeViewState");
        state.SaveTreeView(MyTreeView, "TreeViewState", HttpContext.Current);
    }

    protected void MyTreeView_TreeNodeExpanded(object sender, EventArgs e)
    {
        if (!IsPostBack) return;
        state = new TreeViewState("TreeViewState");
        state.SaveTreeView(MyTreeView, "TreeViewState", HttpContext.Current);
    }

The above approach is driven from the http://code.msdn.microsoft.com/CSASPNETMaintainTreeViewSta-c7673683

I have a TreeView on my master page and I keep the state in a cookie so navigating to different pages doesn't always collapse the navigation tree.

In the master page, I store the state of the tree before the page is unloaded:

<script type="text/javascript" language="javascript">
        window.onbeforeunload = function () {
            try {
                // The expanded state of the treeview is available in the hidden input
                // ..._ExpandState. Store that string in a cookie so we have it on the server
                // side when the page is loaded in order to restore the state of the treeview.
                var expInfo = document.getElementById('<%=TreeView1.ClientID%>' + '_ExpandState');
                if (expInfo)
                {
                    createCookie('ToolsTVExpand', expInfo.value, 365);
                }
            }
            catch (err) {
                // Ignore the error
            }
        }
        function createCookie(name, value, days) {
            if (days) {
                var date = new Date();
                date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
                var expires = "; expires=" + date.toGMTString();
            }
            else var expires = "";
            document.cookie = name + "=" + value + expires + "; path=/";
        }
</script>

Then in the code behind:

protected void Page_Load(object sender, EventArgs e)
{
  ....
  // If this is the initial loading of the page, restore the navigation tree view state
  if (!IsPostBack)
      reloadTreeviewState();
}

/*
 * The Tools treeview state is stored in a cookie by the client before the user navigates
 * away from the page. It's in the form of one character per node with 'e' being expanded.
 */
public void reloadTreeviewState()
{
    try
    {
        HttpCookie cookie = Request.Cookies["ToolsTVExpand"];
        if (cookie != null && cookie.Value != null)
        {
            int currIdx = 0;
            foreach (TreeNode mainNode in TreeView1.Nodes)
            {
                currIdx = setNodeStates(mainNode, cookie.Value, currIdx);
            }
        }
    }
    catch (Exception e)
    {
        // Just keep going
    }
}
protected int setNodeStates(TreeNode node, String stateInfo, int currIdx)
{
    if (currIdx >= stateInfo.Length)
        throw new Exception("State info shorter than list of nodes.");
    Char state = stateInfo[currIdx];`enter code here`
    if (state == 'e')
    {
        node.Expand();
    }
    currIdx++;
    if (node.ChildNodes != null && node.ChildNodes.Count != 0)
    {
        foreach (TreeNode childNode in node.ChildNodes)
        {
            currIdx = setNodeStates(childNode, stateInfo, currIdx);
        }
    }

    return currIdx;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM