Florence Blogspot about asp.net tutorials and web design and web development

Monday, March 16, 2009

saving TreeView's state

I write a general routine that does the state caching, but it was for leaving a page and coming back to it. (i.e., select a node, then click "Edit" -- which shoots you to an "Edit" page that's not in a hidden panel on the same page... after you're done editing, you get Response.Redirect-ed back to the TreeView page and the expand/collapse/selected-node state is all gone... that's the issue this addresses.)

It's implemented in two static methods that I popped into a "TreeViewHelper" static class. Whenever you need to leave the page, call the "StoreTreeViewStateToSession" method. In your method to bind data to your TreeView, call RestoreTreeViewStateFromSession. If there's no stored state, it just returns silently. If there is stored state, it'll restore it (which nodes are expanded and collapsed, and which node is selected).

They use Session state, which seems appropriate since it's obviously by-user, and cross-page. But the Session variable key has the page name embedded in it, which would cause it *not* to work in your solution (since it's a different page trying to restore the state). A minor tweak to this routine would remedy that issue, however. Store as the variable name suffix a "key" common to your PageA, PageB, etc., instead of Request.ServerVariables["path_info"].

Here's the code. I've just recently adapted it from the IEWebControls TreeView add-on (circa .NET 1.0) and updated it to work with the ASP.NET 2.0 TreeView control. It seems to work like a charm:

public static void StoreTreeViewStateToSession(TreeView tvIn)
{
// Takes the TreeView's state and saves it in a Session variable
// Call this method before leaving the page if we expect to be back
string strVarName;
string strList = "";

strVarName = "tv_" + HttpContext.Current.Request.ServerVariables["path_info"];
if (HttpContext.Current.Session[strVarName] + "" != "")
{
HttpContext.Current.Session.Remove(strVarName);
}

StoreTreeViewStateToSession_Recurse(tvIn.Nodes[0], ref strList);

strList = tvIn.SelectedNode.ValuePath + strList;

HttpContext.Current.Session.Add(strVarName, strList);
}


private static void StoreTreeViewStateToSession_Recurse(TreeNode tnIn, ref string strList)
{
if (tnIn.Expanded == true)
{
strList = "," + tnIn.ValuePath + strList;
}
foreach (TreeNode tnCurrent in tnIn.ChildNodes)
{
StoreTreeViewStateToSession_Recurse(tnCurrent, ref strList);
}
}


public static void RestoreTreeViewStateFromSession(TreeView tvIn)
{
// Takes the Session-stored TreeView state and restores it
// to the passed-in TreeView control.
// Call this method on entry to the page. Nothing will
// happen if the variable doesn't exist.
string strVarName;

// See if stored data exists for this treeview
strVarName = "tv_" + HttpContext.Current.Request.ServerVariables["path_info"];
if (HttpContext.Current.Session[strVarName] + "" != "")
{
string strSelectedNodeIndex = "";
foreach (string strCurrent in HttpContext.Current.Session[strVarName].ToString().Split(','))
{
if (strSelectedNodeIndex == "") // First element in list is selected node
{
strSelectedNodeIndex = strCurrent;
}
else
{
try
{
tvIn.FindNode(strCurrent).Expanded = true;
}
catch
{
//eat exception
}
}
}

try
{
// Verify that node exists before setting SelectedNodeIndex
TreeNode tnTest = tvIn.FindNode(strSelectedNodeIndex);

// Select the node (will only happen if it exists)
tvIn.FindNode(strSelectedNodeIndex).Select();

// Ensure the selected node's parent is expanded
((TreeNode)tvIn.FindNode(tvIn.SelectedNode.ValuePath).Parent).Expanded = true;

}
catch
{
// eat exception
}

HttpContext.Current.Session.Remove(strVarName);
}
}



... Hope that helps!!! See my other post on client-side node selection without forcing PostBack (http://forums.asp.net/thread/1452479.aspx) if you're interested in another of my TreeViewHelper tools, which I'm pretty pleased with lately. Microsoft so needs to hire me. ;-)

No comments:

Search