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

Failed to load viewstate

6 Answers 227 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Mina
Top achievements
Rank 1
Mina asked on 01 Mar 2010, 09:25 AM

Hi.
I am not good at English.

I use RadTreeView for Ajax 2009 Q3.
I try to maintain a state of RadTreeView Nodes before creating a new folder via right-clicking menu.
The sorce code is the following.


namespace TelerikTreeView  
{  
    public partial class _Default : System.Web.UI.Page  
    {  
        /// <summary>  
        /// Variable for state maintein  
        /// </summary>  
        private RadTreeNodeCollection preChangeTreeNodes;  
          
        protected void RadTreeView1_ContextMenuItemClick(object sender, RadTreeViewContextMenuEventArgs e)  
        {  
            RadTreeNode clickedNode = e.Node;  
 
            // Copy Nodes  
            this.CopyRadTreeNodesTo(out preChangeTreeNodes);  
            .....  
        }  
 
        /// <summary>  
        /// The state of present TreeView is copied onto the RadTreeNodeCollection object of the argument.  
        /// </summary>  
        /// <param name="nodes"></param>  
        private void CopyRadTreeNodesTo(out RadTreeNodeCollection nodes)  
        {  
            nodes = new RadTreeNodeCollection(this.RadTreeView1);  
            foreach (RadTreeNode node in this.RadTreeView1.Nodes)  
            {  
                nodes.Add(node.Clone());  
            }  
        } 

The following error occurs by the second post backing.

-----------------------
Failed to load viewstate.  The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request.  For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.
-----------------------

The error will not occurs when EnableViewState property of TreeView is false.
But I don't want to change EnableViewState to false. Beause I can not get RadTreeViewContextMenuEventArgs Value when it is false.
Also SessionState mode is SQLServer, I can not use session to maintain the node.
How to keep the node during page post-back?

6 Answers, 1 is accepted

Sort by
0
Schlurk
Top achievements
Rank 2
answered on 01 Mar 2010, 03:32 PM
If you look at the post marked as answer in this thread (from the ASP.NET forums) you can see a little bit of more information regarding exactly why this is occurring. I'm not exactly sure how you are storing this in the ViewState or how you are later handling this information but it would seem as if the way you are handling the ViewState and loading the TreeView is what's causing this. Are you loading the TreeView dynamically on every page load or how are you using the "unchanged" version of the TreeView?
0
Mina
Top achievements
Rank 1
answered on 03 Mar 2010, 06:24 AM

Thank you for Reply.

TreeView nodes is preserved it in the data base.
PageLoad, preChangeTreeNodes save TreeView nodes.
Then right-clicking, I preserve TreeView nodes in DB.
At this time, the display of TreeView is changed by the value of preChangeTreeNodes when there is a difference at time to have loaded the node of TreeView first and the time that exists in present DB.

How to keep the node during page post-back?

0
Accepted
Peter
Telerik team
answered on 08 Mar 2010, 04:38 PM
Hello Mina,

You can get the state using the GetXml() method and then you can recreate the same treeview using the LoadXmlString() method. Here is a simple example:

<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
 <telerik:RadTreeView ID="RadTreeView1" runat="server">
     <Nodes>
         <telerik:RadTreeNode runat="server" Text="Root RadTreeNode1">
             <Nodes>
                 <telerik:RadTreeNode runat="server" Text="Child RadTreeNode 1">
                 </telerik:RadTreeNode>
                 <telerik:RadTreeNode runat="server" Text="Child RadTreeNode 2">
                     <Nodes>
                         <telerik:RadTreeNode runat="server" Text="Child RadTreeNode 1">
                         </telerik:RadTreeNode>
                         <telerik:RadTreeNode runat="server" Text="Child RadTreeNode 2">
                         </telerik:RadTreeNode>
                     </Nodes>
                 </telerik:RadTreeNode>
             </Nodes>
         </telerik:RadTreeNode>
         <telerik:RadTreeNode runat="server" Text="Root RadTreeNode2">
         </telerik:RadTreeNode>
         <telerik:RadTreeNode runat="server" Text="Root RadTreeNode3">
             <Nodes>
                 <telerik:RadTreeNode runat="server" Text="Child RadTreeNode 1">
                 </telerik:RadTreeNode>
                 <telerik:RadTreeNode runat="server" Text="Child RadTreeNode 2">
                 </telerik:RadTreeNode>
             </Nodes>
         </telerik:RadTreeNode>
     </Nodes>
 </telerik:RadTreeView>
    <telerik:RadTreeView ID="RadTreeView2" runat="server">
 </telerik:RadTreeView>

protected void Button1_Click(object sender, EventArgs e)
    {
        RadTreeView2.LoadXmlString(RadTreeView1.GetXml());
    }

I hope this helps.

All the best,
Peter
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.
0
Mina
Top achievements
Rank 1
answered on 09 Mar 2010, 08:10 AM
Thank you for your sample, Peter .
I use "GetXml()" and "LoadXmlString()", then I can keep the node during page post-back.

Thank you !
0
Sunil
Top achievements
Rank 1
answered on 20 Jul 2010, 09:18 PM
For anyone reading this I've wasted a full day with this problem but have learned a lesson.

I'm not going to go into the details of it all but if you're having this viewstate problem try putting PlaceHolders around your controls. This simple method fixed my problems.

I hope this helps somebody else out there!!
0
raju
Top achievements
Rank 1
answered on 27 Jun 2017, 04:58 AM

Hi team,

i want to maintain the RadTreeView data during postbacks in a javascript.and i gave expandmode=3(webservice) to get a child nodes for the corresponding nodes.

Tags
TreeView
Asked by
Mina
Top achievements
Rank 1
Answers by
Schlurk
Top achievements
Rank 2
Mina
Top achievements
Rank 1
Peter
Telerik team
Sunil
Top achievements
Rank 1
raju
Top achievements
Rank 1
Share this question
or