Welcome Guest Search | Active Topics | Sign In | Register

Treeview in Masterpage not save State Options
kennaxx
Posted: Friday, May 15, 2009 10:39:08 AM
Rank: Advanced Member
Groups: Member

Joined: 4/30/2009
Posts: 36

I have a Treeview which saves state across pages when NOT in a masterpage. However, when embedded in a masterpage, it does not save state. Do I need to save state in session and then restore it from the session after postback?

Thanks

Kennaxx
eo_support
Posted: Friday, May 15, 2009 10:46:52 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

No. The TreeView should not have a problem with master page in general, so it's more likely has to do with your particular scenario. The most likely problem is the ClientID changes across different pages. The TreeView must have exactly same ID in all pages.

Also I do not think save state cross page works when the TreeView raises server event. It only works when the TreeView navigate to another page through NavigateUrl.

If the problem continues, please create a test project to reproduce the problem. Once we have that we will debug it here and see what we can find.

Thanks!
kennaxx
Posted: Friday, May 15, 2009 3:49:29 PM
Rank: Advanced Member
Groups: Member

Joined: 4/30/2009
Posts: 36
EO,

This is the code that I have to save/restore the Treeview before/after posts. The node.Childnodes.count keeps coming out count of 0 eventhough I am 100% sure it should not be 0.

public class EOTreeViewState
{
public void SaveTreeView(EO.Web.TreeView treeView, string key)
{
List<bool?> list = new List<bool?>();
SaveTreeViewExpandedState(treeView.Nodes, list);
HttpContext.Current.Session[key + treeView.ID] = list;
}

private void SaveTreeViewExpandedState(EO.Web.TreeNodeCollection nodes, List<bool?> list)
{
foreach (EO.Web.TreeNode node in nodes)
{
list.Add(node.Expanded);
if (node.ChildNodes.Count > 0)
{
SaveTreeViewExpandedState(node.ChildNodes, list);
}
}
}

private int RestoreTreeViewIndex;

public void RestoreTreeView(EO.Web.TreeView treeView, string key)
{
RestoreTreeViewIndex = 0;
RestoreTreeViewExpandedState(treeView.Nodes,
(List<bool?>)HttpContext.Current.Session[key + treeView.ID] ?? new List<bool?>());
}

private void RestoreTreeViewExpandedState(EO.Web.TreeNodeCollection nodes, List<bool?> list)
{
foreach (EO.Web.TreeNode node in nodes)
{
if (RestoreTreeViewIndex >= list.Count) return;

node.Expanded = (bool)list[RestoreTreeViewIndex++];
if (node.ChildNodes.Count > 0)
{
RestoreTreeViewExpandedState(node.ChildNodes, list);
}
}
}
}

Thanks Kennaxx
eo_support
Posted: Friday, May 15, 2009 4:13:31 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

I am not quite clear about what you wanted to save. Your function saves/restores node state, but not the node themselves.

TreeView automatically saves/restores all nodes and state for nodes state through view state. When you navigate from one page to another, view state is discarded. So in this case it does not restore nodes that you have dynamically created. For example, if you have a blank TreeView in your master page, and Button1 and Button2 in Page1 (Page1 ueses master page) and:

1. Button1 adds a new TreeNode into the TreeView. So after Button1 is clicked, TreeView has one node;
2. Button2 redirects to Page2 (which also use the same master page). Once the page is redirected to Page2, TreeView will be blank again;

This is normal. If you wish the TreeView to retain all nodes created in Page1, you will need to save it somehow. For example, you can save it in the Session or in the database.

Thanks!


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.