'Maintain Treeview position in asp.net

I'm using Treeview in asp.net

Treeview like

PARENT1

    #Child
       .Subchild1
       .Subchild2

PARENT2
    #child1
    #child2

first when my page load at first time all nodes should be collapsed like

PARENT1
PARENT2

If I expand PARENT1 and click subchild2 my page should be redidplayed and parent1 should be expanded and parent2 should be collapsed....

if I click parent2 viceversa...

so I have to maintain tree position level in every postback any solution for this?

I'm using the following code but I got error

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["tvExpandNode1"] != null)
    {
        TreeView1.FindNode(Session["tvExpandNode1"].ToString()).Expand();
    }
}

protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
    if (TreeView1.SelectedNode.Expanded==true)
    {
        Session["tvExpandNode1"] = TreeView1.SelectedNode.Parent.Parent.Value;
        if (strOpenpage == "Report.aspx")
        {
            OpenNewWindow(strOpenpage);
        }
        else
        {
            Response.Redirect(strOpenpage, false);
        }
    }
}
 


Solution 1:[1]

Yes, you can write code to do this with the standard ASP.NET treeview to do this. I know some controls support binding to a sitemap and this control should be able to be bound to a sitemapdatasource, if your tree is related to the current page hierarchy. I don't know that that gives you automatic expand/collapse based upon the selected node. You could toy with that if your tree is based on the sitemap. Otherwise, programmatically this can be handled, but haven't heard of anything to give you this out of the box...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Brian Mains