Nesscafe
Dec 6th, 2007, 01:27 AM
i have a problem with a session and i cant figure it out on my own :( so i hope someone can help me. ok,here's the problem: i have a TreeViewState.cs that saves the state of the treeview and expands the nodes and a collapse code, they work fine with except one thing, the state is saved with the node expanded after the node is visited, but in the same time if i go to another node the collapse code goes into action and collapses the previous node. the problem is that when i want to revisit a previous node it wont expand because it's saved expanded, and not collapses as it should be. how can i modify the SaveState class so it will "update" the state of the nodes, or the collapse code.they stay on a master page and the treeview is generated via db, no xml file or SitemapDataSource exist.Other info: the nodes have a url property, and the the redirect is made by a server.transfer code. any help would be great. THX
public class TreeViewState
{
public static bool IsTreeViewStateSaved(TreeView LinksTreeView, string key)
{
bool isSaved = (HttpContext.Current.Session[key + LinksTreeView.ID] != null);
return isSaved;
}
public static void SaveTreeView(TreeView LinksTreeView, string key)
{
HttpContext.Current.Session[key + LinksTreeView.ID] = LinksTreeView.Nodes;
}
public static void RestoreTreeView(TreeView LinksTreeView, string key)
{
if (IsTreeViewStateSaved(LinksTreeView, key))
{
LinksTreeView.Nodes.Clear();
TreeNodeCollection nodes = (TreeNodeCollection)HttpContext.Current.Session[key + LinksTreeView.ID];
for (int index = nodes.Count - 1; index >= 0; index += -1)
{
LinksTreeView.Nodes.AddAt(0, nodes[index]);
}
HttpContext.Current.Session[key + LinksTreeView.ID] = null;
}
}
}
collapse code:
protected void TreeView1_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
{
if (e.Node.Parent == null)
return;
string strNodeValue = e.Node.Value;foreach (TreeNode node in e.Node.Parent.ChildNodes)
{
if (node.Value != strNodeValue)
{
node.Collapse();
HttpContext.Current.Session.Clear();
}
}
}
public class TreeViewState
{
public static bool IsTreeViewStateSaved(TreeView LinksTreeView, string key)
{
bool isSaved = (HttpContext.Current.Session[key + LinksTreeView.ID] != null);
return isSaved;
}
public static void SaveTreeView(TreeView LinksTreeView, string key)
{
HttpContext.Current.Session[key + LinksTreeView.ID] = LinksTreeView.Nodes;
}
public static void RestoreTreeView(TreeView LinksTreeView, string key)
{
if (IsTreeViewStateSaved(LinksTreeView, key))
{
LinksTreeView.Nodes.Clear();
TreeNodeCollection nodes = (TreeNodeCollection)HttpContext.Current.Session[key + LinksTreeView.ID];
for (int index = nodes.Count - 1; index >= 0; index += -1)
{
LinksTreeView.Nodes.AddAt(0, nodes[index]);
}
HttpContext.Current.Session[key + LinksTreeView.ID] = null;
}
}
}
collapse code:
protected void TreeView1_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
{
if (e.Node.Parent == null)
return;
string strNodeValue = e.Node.Value;foreach (TreeNode node in e.Node.Parent.ChildNodes)
{
if (node.Value != strNodeValue)
{
node.Collapse();
HttpContext.Current.Session.Clear();
}
}
}