Finding myself without a nice example of how to accomplish this, there was no other way of doing this myself :-)
First, we need to find out which nodes are expanded. The right time to check for this is when nodes are expanded/collapsed. So here is the client-side function:
function uxProfileTree_NodeToggle(sender, args) {
var tree = $find("<%= uxProfileTree.ClientID %>");
var allNodes = tree.get_allNodes();
var nodeString = "";
for (var i = 0; i < allNodes.length; i++) {
var node = allNodes[i];
var expanded = node.get_expanded();
if (expanded) {
nodeString = nodeString + node.get_value() + "*";
createCookie("preofileTreeState", nodeString, 365);
}
}
}
This function is hooked into the OnClientCollapsed and OnClientExpanded client-side handlers of the treeview that you want to persist.
We now know what nodes are expanded so lets put the nodeString to work. In the Page_LoadComplete handler of my ASP.NET page I check for the cookie we created on the client and parse its value into an array by splitting the nodeString at the * char.
HttpCookie cookie = Request.Cookies[treeCookieName];
try
{
if (cookie != null)
{
string[] toggleParts = cookie.Value.Split(new[] { "*" }, StringSplitOptions.RemoveEmptyEntries);
foreach (string part in toggleParts)
{
RadTreeNode toggledNode = uxProfileTree.FindNodeByValue(part);
toggledNode.Expanded = true;
}
}
}
catch
{
// Remove the cookie if it messes up!
Request.Cookies.Remove(treeCookieName);
}
For every value we find in the array, we look up the corresponding node in the tree. The only thing left is setting the node’s Expanded property to true. As a failsafe, the cookie is removed if anything messes up while trying to find the matching node. This is to make sure the user isn’t stuck with a broken cookie and a tree that wont expand its nodes.