title: Maintain the state of collasped/Expanded node in treeview control in asp.net
body: <p>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 </p>
  
  <pre><code>  &lt;asp:TreeView Height="100%" ID="MyTreeView" OnTreeNodeCollapsed="MyTreeView_TreeNodeCollapsed" OnTreeNodeExpanded="MyTreeView_TreeNodeExpanded"  /&gt;
  
  
    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);
      }
  </code></pre>
  
  <p>The above approach is driven from the <a href="http://code.msdn.microsoft.com/CSASPNETMaintainTreeViewSta-c7673683" rel="nofollow">http://code.msdn.microsoft.com/CSASPNETMaintainTreeViewSta-c7673683</a></p>