This is a migrated thread and some comments may be shown as answers.

RadTreeView State

3 Answers 139 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Vme
Top achievements
Rank 1
Vme asked on 02 Mar 2009, 10:25 PM
Hello,

I'm trying to maintain the state of my RadTreeView in my master page as i navigate from content page to content page.  I have everything working except for the face that when I collapse my nodes and switch pages they open back up when I change content pages.  I know its because I'm using clientside for my TreeNodeExpandMode, but if I switch it to ServerSideCallBack once the post back is completed even though the nodes say Expanded="True" I get a '+' sign and it loads on demand again. I just have two hyperlinks on my page that switch from Page1.aspx and Page2.aspx.  Page1/2.aspx both use Site1.Master as their master page.   Any suggestions would be great!  Thank you.  

Here is my master page code:

public partial class Site1 : System.Web.UI.MasterPage 
    { 
        protected void Page_Load(object sender, EventArgs e) 
        { 
            if (!Page.IsPostBack && String.IsNullOrEmpty((string)Session["treeViewState"])) 
            { 
                LoadRootNodes(); 
                Session["treeViewState"] = RadTreeView1.GetXml(); 
            } 
            else if (!String.IsNullOrEmpty((string)Session["treeViewState"])) 
            { 
                this.RadTreeView1.LoadXml((string)Session["treeViewState"]); 
            } 
        } 
 
        private void LoadRootNodes() 
        { 
            for (int i = 0; i < 5; i++) 
            { 
                this.RadTreeView1.Nodes.Add(new RadTreeNode() 
                { 
                    Text = String.Format("Root: {0}", i.ToString()), 
                    Value = i.ToString(), 
                    ExpandMode = TreeNodeExpandMode.ServerSideCallBack 
                }); 
            } 
        } 
 
        private void AddChildNodes(RadTreeNode node) 
        { 
            for (int i = 0; i < 3; i++) 
            { 
                node.Nodes.Add(new RadTreeNode() 
                { 
                    Text = String.Format("{0}{1}",node.Level.ToString(), i.ToString()), 
                    Value = String.Format("{0}{1}",node.Level.ToString(), i.ToString()), 
                    ExpandMode = TreeNodeExpandMode.ServerSideCallBack 
                }); 
            } 
        } 
 
        protected void RadTreeView1_NodeExpand(object sender, RadTreeNodeEventArgs e) 
        { 
            AddChildNodes(e.Node); 
 
            string treeViewState = (string)Session["treeViewState"]; 
            RadTreeView cachedTreeView = new RadTreeView(); 
            cachedTreeView.LoadXmlString(treeViewState); 
            RadTreeNode cachedNodeClicked = cachedTreeView.FindNodeByValue(e.Node.Value); 
             
            AddChildNodes(cachedNodeClicked); 
            cachedNodeClicked.ExpandMode = TreeNodeExpandMode.ClientSide; 
            cachedNodeClicked.Expanded = true
 
            Session["treeViewState"] = cachedTreeView.GetXml(); 
        } 
    } 

3 Answers, 1 is accepted

Sort by
0
Yana
Telerik team
answered on 04 Mar 2009, 01:05 PM
Hello,

I've modified your code so that it saves the state of the treeview now. Note that you should use the server-side onNodeCollapse event in order to change the xml when a node is collapsed. Please download the attached file and give it a try.

Kind regards,
Yana
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
SoniTek
Top achievements
Rank 1
answered on 22 Apr 2010, 11:38 PM
Do you have a sample like the one above in load treeview from xml file?
0
T. Tsonev
Telerik team
answered on 23 Apr 2010, 03:19 PM
Hello,

Please, refer to this example for populating the TreeView from XML:

http://demos.telerik.com/aspnet-ajax/treeview/examples/programming/xmlfile/defaultcs.aspx

I hope this helps.

All the best,
Tsvetomir Tsonev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
TreeView
Asked by
Vme
Top achievements
Rank 1
Answers by
Yana
Telerik team
SoniTek
Top achievements
Rank 1
T. Tsonev
Telerik team
Share this question
or