views:

1378

answers:

5

I am using ASP.NET with C# 2.0 and Visual Studio 2005. I am using a Master page and content pages. I have a treeview menu in the master page and when a user selects any menu item I redirect to that content page.

My problem is that after a user navigates to the content page all the treenodes refresh and the structure is collapsed. I want the selected treenode to stay expanded.

Can anybody help me out?

+2  A: 

When you refresh the treeview you want to call treeView1.ExpandAll();

Also add an event for the BeforeCollapse and set the event's Cancel property to true, to prevent the user from collapsing your treenodes.

private void treeView1_BeforeCollapse(object sender, TreeViewCancelEventArgs e)
{
    e.Cancel = true;
}

Hope this helps.

-jeremy

where can i write your code please ?
Kartik
A: 

Try using the OnTreeNodeDataBound event and the treeView.SelectedNode property

Also, might want to check how/ when you're binding your TreeView to it's DataSource. You might be rebinding it on IsPostBack which will re-render the tree.

The TreeView should maintain its nodes on PostBack.

hunter
A: 

Even though you are using a Master page, once the user navigates to the content page it is rendered as a new/different page. Because of the Master page the same treeview is loaded but not the same instance. You will need to store and load what nodes were expanded.

Shaun Humphries
A: 

This is a common enough problem that is automatically handled by ASP.NET if you use a SiteMapDataSource control as the datasource for your TreeView. In this case, you haven't mentioned what the Datasource of your TreeView is.

You also haven't mentioned if the TreeView contains links (the NavigateUrl property is set) or Text items that postback for each click. If it is the former, then as far as I know, you are out of luck! This is because none of the Selection events are raised for TreeNodes which have their NavigateUrl set. They just function as regular hyperlinks.

If however, it is the latter, then you can try out the following steps :

a. Handle the SelectedNodeChanged event of the TreeView. In this event handler, retrieve the current value of the SelectedNode.ValuePath property and store it in ViewState/Session. Use the Value of the of the SelectedNode to conditionally redirect the page to URL mapped to it.

Something like the following:

protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e) 
{ 
  TreeNode selNode = TreeView1.SelectedNode; 
  string pathToNode = selNode.ValuePath; 
  Session.Add("SelPath", pathToNode); 

  switch (selNode.Value) 
  { 
  //Redirect to URL accordingly. 
  } 
}

b. On subsequent load of the Master page (the page to which you redirected), retrieve the value of the ValuePath set earlier and find the previously Selected node and Expand it.

Something like the following:

protected void Page_Load(object sender, EventArgs e) 
{ 
  if (Page.IsPostBack)
  { 
    string pathToNode = (string)Session("SelPath"); 
    Session.Remove("SelPath"); 
    TreeNode selNode = TreeView1.FindNode(pathToNode); 
    if (selNode != null) 
    { 
      selNode.Expand(); 
    } 
  } 
}

Note that I haven't had an opportunity to test the code so this is mostly hypothetical.

Cerebrus