Hello.
The treeview has two methods (LoadNodeState and SaveNodeState) which are used to persist state between calls. Unfortunatelly, these methods are private so there's no way you can call them directly. However, you still have reflection. I've built a small sample which saves and loads the state of the tree view to a file. I know for sure that if i have more than 1 top node the sate isn't loaded correctly(hum...or is it saved?) . Everything seems to work out great if I only have one top node. I'd really love to study this a little better and see if I can find what's wrong. Unfortunatelly, I really don't have more time for it now.
I've tried to reuse the existing code; however, if I were you I'd build my own schema (which could be similar to the one used by the methods LoadNodeState and SaveNodeState).
Here's the code:
<%@ Page Language="C#" %>
<%@ Register Namespace="Test" TagPrefix="LA" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.Globalization" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">
void SaveInfoToFile( object sender, EventArgs e )
{
base.OnPreRender( e );
StringBuilder builder1 = new StringBuilder( );
int num1 = 0;
Type tp = typeof( TreeView );
for ( int num2 = 0; num2 < tree.Nodes.Count; num2++ )
{
tp.InvokeMember( "SaveNodeState",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.InvokeMethod |
System.Reflection.BindingFlags.Instance, null, tree,
new object [] { tree.Nodes [num2], num1, builder1, true } );
//this.SaveNodeState( this.Nodes [num2], ref num1, builder1, true );
}
string aux = builder1.ToString( );
FileStream file = new FileStream( "g:\\mybook\\t.txt", FileMode.OpenOrCreate );
StreamWriter str = new StreamWriter( file );
str.Write( aux.ToString( ) );
str.Close( );
}
void LoadInfoFromFile( object sender, EventArgs args )
{
IDictionary dictionary1 = new HybridDictionary( );
ArrayList list1 = new ArrayList( );
//this.PopulateLogID = this.ClientID+"_PopulateLog"; should use invokemember, but no time ;)
string text2 = this.Context.Request [tree.ClientID + "_PopulateLog"];
if ( text2 != null )
{
char [] chArray1 = new char [1] { ',' };
string [] textArray1 = text2.Split( chArray1 );
for ( int num2 = 0; num2 < textArray1.Length; num2++ )
{
int num3;
if ( ( textArray1 [num2].Length > 0 ) && int.TryParse( textArray1 [num2], NumberStyles.Integer, CultureInfo.InvariantCulture, out num3 ) )
{
list1.Add( num3 );
dictionary1.Add( num3, string.Empty );
}
}
}
FileStream file = new FileStream( "g:\\mybook\\t.txt", FileMode.OpenOrCreate );
StreamReader reader = new StreamReader( file );
string aux = reader.ReadToEnd( );
reader.Close( );
Type tp = typeof( TreeView );
int num4 = 0;
int num1 = -1;
for ( int num5 = 0; num5 < tree.Nodes.Count; num5++ )
{
//this.LoadNodeState( this.Nodes [num5], ref num4, text3, dictionary1, num1 );
tp.InvokeMember( "LoadNodeState",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.InvokeMethod |
System.Reflection.BindingFlags.Instance, null, tree,
new object [] { tree.Nodes [num5], num4, aux, dictionary1, num1} );
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:TreeView runat="server" ID="tree" EnableViewState="false">
<Nodes>
<asp:TreeNode Text="Test">
<asp:TreeNode Text="child1">
<asp:TreeNode Text="subchild1" />
<asp:TreeNode Text="subchild1" />
</asp:TreeNode>
<asp:TreeNode Text="child5">
<asp:TreeNode Text="subchild3" />
<asp:TreeNode Text="subchild4" />
</asp:TreeNode>
<asp:TreeNode Text="child2" />
</asp:TreeNode>
</Nodes>
</asp:TreeView>
<asp:Button ID="SavetoFile" runat="server" Text="Save To File" OnClick="SaveInfoToFile" />
<asp:Button ID="LoadFromFile" runat="server" Text="Load From File" OnClick="LoadInfoFromFile" />
<asp:Button ID="Button1" runat="server" Text="Load From File" />
</form>
</body>
</html>
Hope it helps you get started.
--
Regards,
Luis Abreu
email: labreu_at_gmail.com
PT blog: http://weblogs.pontonetpt.com/luisabreu
EN blog:http://msmvps.com/blogs/luisabreu
http://www.pontonetpt.com
http://weblogs.pontonetpt.com/