View State Control Tree miss-match

2 posts, 0 answers
  1. Pirooz
    Pirooz avatar
    6 posts
    Member since:
    Nov 2007

    Posted 08 Jan 2012 Link to this post

    Hell all,

    I'm trying to open a RadWindow  with some dynamic behavior inside the RadWindow (grids and forms).  I created a sample project and am able to get the RadWindow open and update controls using Ajax by setting the EnableViewState to false on almost all the controls inside the RadWindow.

    Now that I've added a Cancel button, I get the error:

    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. 

    Here is the snippets to the sample project i created for test purposes:

    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
            <telerik:AjaxSetting AjaxControlID="btnGetDate" >
                    <telerik:AjaxUpdatedControl ControlID="datePanel" />
           <telerik:AjaxSetting AjaxControlID="RadToolBar1" >
                    <telerik:AjaxUpdatedControl ControlID="windowPanel" />
    <telerik:RadToolBar ID="RadToolBar1" Runat="server"
            <telerik:RadToolBarButton runat="server" Text="Window">
    <asp:Panel ID="windowPanel" runat="server">
        <telerik:RadWindowManager ID="RadWindowManager1" runat="server">
        <telerik:RadWindow ID="RadWindow1" runat="server" Modal="true" >
                <asp:Panel ID="datePanel" runat="server">
                    Date: <asp:Literal ID="litDate" runat="server" EnableViewState="false"></asp:Literal><br />
                    <asp:Button ID="btnGetDate" runat="server" Text="Update Date" OnClick="GetDate_Click" EnableViewState="false" />
                    <br />
                    <asp:Button ID="btnClose" runat="server" Text="Cancel" OnClick="btnClose_Click" EnableViewState="false" />

    The code behind looks like this:

    protected void RadToolBar1_ButtonClick(object sender, Telerik.Web.UI.RadToolBarEventArgs e)
        RadWindow1.VisibleOnPageLoad = true;
        litDate.Text = "not set";
    protected void GetDate_Click(object sender, EventArgs e)
        litDate.Text = DateTime.Now.ToLongTimeString();
        RadWindow1.VisibleOnPageLoad = true;
    protected void btnClose_Click(object sender, EventArgs e)
        RadWindow1.VisibleOnPageLoad = false;

    Any help would be appreciated.
  2. Andrey
    Andrey avatar
    836 posts

    Posted 09 Jan 2012 Link to this post

    Hi Pirooz,

    The error you are getting is caused from the btnClose button. This is true because you have defined the Window control in the ASPX page and when the page gets postbacked to the server the ViewState is trying to recreate the Window object bu if the btnClose button was pressed this control is no longer in the Controls collection of the page and this is broking the ViewState.

    RadWindow solely depends on JavaScript, it is opened on the Client with JavaScript and it is closed also by JavaScript on the client. So, adding and removing it from the server Controls collection is not necessary at all. You could check this help topic for more information on how to execute JavaScript code from server. This forum thread describes how to open RadWindow from the server.

    Following these approaches will help you resolve the issue with the ViewState and to optimize your code.

    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Back to Top