CodeVerge.Net Beta
Login Idy
Register Password
  Forgot?
   Explore    Item Entry    Profile    Invite   History    Resources   SiteMap  
NEWSGROUP
.NET
Algorithms-Data Structures
Asp.Net
C Plus Plus
CSharp
Database
HTML
Javascript
Linq
Other
Regular Expressions
VB.Net
XML




Zone: > newsgroup > asp.net forum > general_asp.net.master_pages_themes_and_navigation_controls Tags:
Item Type: NewsGroup Date Entered: 3/18/2005 11:34:05 AM Date Modified: Subscribers: 0 Subscribe Alert
Rate It:
(NR, 0)
XPoints: N/A Replies: 3 Views: 4 Favorited: 0 Favorite
Can Reply:  No Members Can Edit: No Online: Yes
4 Items, 1 Pages 1 |< << Go >> >|
bjartn
Asp.Net User
Saving the state of the TreeView3/18/2005 11:34:05 AM

0/0

Is is possible to save the state of the treeview (which nodes are expanded and not) and load the treeview with this state at a later time? How do you do this?

I would like to save the state of the tree in the profile or session.
Luis Abreu
Asp.Net User
Re: Saving the state of the TreeView3/22/2005 12:06:07 AM

0/0

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/
jdixon
Asp.Net User
Re: Saving the state of the TreeView3/22/2005 6:31:16 PM

0/0

You can do this using viewstate of the control. This is not a suggested practice as your underlying tree, if changed, will mismatch the data stored. Additionally the pretected APIs used could change...lastly you will want to handle the loading OnLoad and only !IsPostback, I used the button example for testing - but you truly only want to do this on pageLoad.



<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.Globalization" %>

<script runat="server">

protected void Button1_Click(object sender, EventArgs e)
{
Type tp = typeof(TreeView);
ObjectStateFormatter osf = new ObjectStateFormatter();
string s = osf.Serialize(tp.InvokeMember("SaveViewState",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.InvokeMethod |
System.Reflection.BindingFlags.Instance, null, TreeView1,
null));

FileStream file = new FileStream("c:\\t.txt", FileMode.Create);
StreamWriter str = new StreamWriter(file);
str.Write(s);
str.Close();
}



protected void Button2_Click(object sender, EventArgs e)
{
Type tp = typeof(TreeView);
FileStream file = new FileStream("c:\\t.txt", FileMode.OpenOrCreate);
StreamReader reader = new StreamReader(file);
string aux = reader.ReadToEnd();
reader.Close();
ObjectStateFormatter osf = new ObjectStateFormatter();
object s = osf.Deserialize(aux);
tp.InvokeMember("LoadViewState",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.InvokeMethod |
System.Reflection.BindingFlags.Instance, null, TreeView1,
new object[] { s });
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:TreeView runat="server" ID="TreeView1" EnableViewState="false">
<Nodes>
<asp:TreeNode Text="Test" Value="Test">
<asp:TreeNode Text="child1" Value="child1">
<asp:TreeNode Text="subchild1" Value="subchild1" />
<asp:TreeNode Text="subchild2" Value="subchild2" />
</asp:TreeNode>
<asp:TreeNode Text="child5" Value="child5">
<asp:TreeNode Text="subchild3" Value="subchild3" />
<asp:TreeNode Text="subchild4" Value="subchild4" />
</asp:TreeNode>
<asp:TreeNode Text="child2" Value="child2" >
<asp:TreeNode Text="New Node" Value="New Node"></asp:TreeNode>
</asp:TreeNode>
</asp:TreeNode>
<asp:TreeNode Text="test2" Value="test2">
<asp:TreeNode Text="testchild" Value="testchild"></asp:TreeNode>
</asp:TreeNode>
</Nodes>

</asp:TreeView>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Save" />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Load" />
</form>
</body>
</html>







This posting is provided "AS IS" with no warranties, and confers no rights.
This posting is provided "AS IS" with no warranties, and confers no rights.
We Are Hiring
harryhigbie
Asp.Net User
Re: Saving the state of the TreeView3/23/2005 9:28:51 PM

0/0

I am given to stupidly simple solutions if I know the limits I have used will not be tested by my user group for the scope of the application in question.

In this case, I just set up arrays of size 15 for each of the six levels of the tree, according to the application. I then recorded the treenode.ID of any node at each level where treenode.expanded = true when the page was submitted. In .NET, these arrays were public within one of the modules. When the user had returned back to the tree selection screen from the Crystal Report that the treeview helped set up, I checked the array to see if the node had been in the expanded list and then expanded it.

When the tree redisplays upon return, it looks as it had when the user left.

Harry
4 Items, 1 Pages 1 |< << Go >> >|



Search This Site:





Other Resources:
Saving Web page with Web Parts location etc into sql - ng.asp-net-forum ... Loading and saving of the page ... about the state of a page into the /// < see cref="System.Web ... keeping the treeview expanded on reload. custom ...
ng.asp-net-forum.general_asp-net-state_management - Web Programming ... Dynamically added TreeView not saving state. 0. 4. 1/4/2008 7:55:57 PM ... save state in a dynamically generated controls. 4. 8. 1/4/2008 3:55:51 PM ...
Plugins/Treeview/treeview - jQuery JavaScript Library Great for href-based state-saving. ... Saves and restores the state of the selected treeview in a cookie with the name "treeview" ...
AspAlliance NewsReader: Posts in microsoft.public.dotnet.framework ... Save the state of a TreeView Replies (2) 7 January 2008 ... Is there a way that I can save the state of the TreeViews nodes so that I can rea...
Andrew's Boiling It Down - Save TreeView Nodes Expansion / Collapse ... Save TreeView Nodes Expansion / Collapse State (CS and VB) ... Next, save the TreeView state by subscribing to the ... save the state of all nodes. ...
TreeView Members (System.Web.UI.WebControls) Saves the state of the TreeView control. (Overrides WebControl..::.SaveViewState ... that indicates whether the server control is saving changes to its view state. ...
Collapse - Tree menu... Persistant w/ cookies, save state However, most other's have the ability to save the state of the menu over ... The product at http://www.treeview.net accomplished this by setting cookies for ...
Nabble - Mono - Gtk# - TreeStore saving state last few months, but I still have not figured out a good way to save the. state of the TreeStore. ... Gtk.TreeView TestView = new Gtk.TreeView (store.Adapter) ...
TreeView Methods (System.Web.UI.WebControls) Saves the state of the TreeView control. (Overrides WebControl..::.SaveViewState ... Signals the TreeView control to notify the ASP.NET application that the state of ...
SaveStateCrossPages Property ... and selected state. TabStrip. Selected tab. TreeView. Selected, Expanded and Checked state ... By default, query string is used to save the state information. ...
9Rays.Net Technical Support • View topic - TreeView doesn't retain state ... the update panel effects this, but my treeview loses the collapsible states across postbacks. ... The treeview saves state as it should. ...
 
All Times Are GMT