Failed to load viewstate

7 posts, 1 answers
  1. Mina
    Mina avatar
    8 posts
    Member since:
    Feb 2008

    Posted 01 Mar 2010 Link to this post

    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?

  2. Schlurk
    Schlurk avatar
    812 posts
    Member since:
    May 2009

    Posted 01 Mar 2010 Link to this post

    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?
  3. Mina
    Mina avatar
    8 posts
    Member since:
    Feb 2008

    Posted 03 Mar 2010 Link to this post

    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?

  4. Answer
    Peter
    Admin
    Peter avatar
    6637 posts

    Posted 08 Mar 2010 Link to this post

    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.
  5. Mina
    Mina avatar
    8 posts
    Member since:
    Feb 2008

    Posted 09 Mar 2010 Link to this post

    Thank you for your sample, Peter .
    I use "GetXml()" and "LoadXmlString()", then I can keep the node during page post-back.

    Thank you !
  6. Sunil
    Sunil avatar
    29 posts
    Member since:
    Jul 2010

    Posted 20 Jul 2010 Link to this post

    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!!
  7. raju
    raju avatar
    2 posts
    Member since:
    Jun 2017

    Posted 26 Jun 2017 Link to this post

    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.

Back to Top