asp.net treeview expand only selected parent
<p>Im using Treeview in asp.net
if i select any parent node then it should be expand and other parents shoulb be collapsed after page postback(navigation)...im using code below.</p>

<pre><code> protected void Page_Load(object sender, EventArgs e)
{
 if (!IsPostBack)
        {
            if (Session["TreeViewState"] != null)
            {
                List&lt;string&gt; list = (List&lt;string&gt;)Session["TreeViewState"];
                RestoreTreeViewState(TreeViewApplicationSetup.Nodes, list);
            }
        }
        else
        {
            List&lt;string&gt; list = new List&lt;string&gt;(100);
            SaveTreeViewState(TreeViewApplicationSetup.Nodes, list);
            Session["TreeViewState"] = list;
        }
}



 private void SaveTreeViewState(TreeNodeCollection nodes, List&lt;string&gt; list)
    {
        Session["TreeViewState"] = null;
        foreach (TreeNode node in nodes)
        {
            if (node.ChildNodes != null)
            {
                if (node.Expanded.HasValue &amp;&amp; node.Expanded == true &amp;&amp; !String.IsNullOrEmpty(node.Text))
                {
                    list.Add(node.Text);
                }
                if (node.ShowCheckBox == true &amp;&amp; node.ChildNodes.Count == 0 &amp;&amp; node.Parent.Expanded == true)
                {
                    if (node.Checked == true)
                    {
                        list.Add(node.ValuePath + "-T");
                    }
                    else
                    {
                        list.Add(node.ValuePath + "-F");
                    }
                }
                SaveTreeViewState(node.ChildNodes, list);
            }
        }
    }
    private void RestoreTreeViewState(TreeNodeCollection nodes, List&lt;string&gt; list)
    {
        foreach (TreeNode node in nodes)
        {

            if (list.Contains(node.Text) || list.Contains(node.ValuePath + "-T") || list.Contains(node.ValuePath + "-F"))
            {
                if (node.ChildNodes != null &amp;&amp; node.ChildNodes.Count != 0 &amp;&amp; node.Expanded.HasValue &amp;&amp; node.Expanded == false)
                {
                    if (node.Parent != null)
                    {
                        if (list.Contains(node.ChildNodes[0].ValuePath + "T") || list.Contains(node.ChildNodes[0].ValuePath + "-F"))
                        {
                            node.Expand();
                        }
                    }
                    else
                    {
                        node.Expand();
                    }
                }
                else if (node.ChildNodes != null &amp;&amp; node.Expanded.HasValue &amp;&amp; node.Expanded == false)
                {
                    if (node.ShowCheckBox == true &amp;&amp; list.Contains(node.Parent.Text) &amp;&amp; list.Contains(node.Parent.Parent.Text))
                    {
                        if (list.IndexOf(node.ValuePath + "-T") != -1)
                        {
                            node.Checked = true;

                        }
                        else if (list.IndexOf(node.ValuePath + "-F") != -1)
                        {
                            node.Checked = false;

                        }
                    }
                }
            }
            else
            {
                if (node.ChildNodes != null &amp;&amp; node.ChildNodes.Count != 0 &amp;&amp; node.Expanded.HasValue &amp;&amp; node.Expanded == true)
                {
                    node.Collapse();
                }
            }
            if (node.ChildNodes != null &amp;&amp; node.ChildNodes.Count != 0)
            {
                RestoreTreeViewState(node.ChildNodes, list);
            }
        }
    }
</code></pre>

<p>its jsut help me to expand parent nodes in every postback but othere parents node didnt collapsed....</p>
