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

View State Control Tree miss-match

1 Answer 90 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Pirooz
Top achievements
Rank 1
Pirooz asked on 08 Jan 2012, 04:28 PM
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">
    <AjaxSettings>
        <telerik:AjaxSetting AjaxControlID="btnGetDate" >
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="datePanel" />
            </UpdatedControls>
        </telerik:AjaxSetting>
       <telerik:AjaxSetting AjaxControlID="RadToolBar1" >
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="windowPanel" />
            </UpdatedControls>
        </telerik:AjaxSetting>
    </AjaxSettings>
</telerik:RadAjaxManager>
 
<telerik:RadToolBar ID="RadToolBar1" Runat="server"
    onbuttonclick="RadToolBar1_ButtonClick">
    <Items>
        <telerik:RadToolBarButton runat="server" Text="Window">
        </telerik:RadToolBarButton>
    </Items>
</telerik:RadToolBar>
 
<asp:Panel ID="windowPanel" runat="server">
    <telerik:RadWindowManager ID="RadWindowManager1" runat="server">
    </telerik:RadWindowManager>
 
    <telerik:RadWindow ID="RadWindow1" runat="server" Modal="true" >
        <ContentTemplate>
            <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" />
            </asp:Panel>
        </ContentTemplate>
    </telerik:RadWindow>
</asp:Panel>

The code behind looks like this:

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

Any help would be appreciated.

1 Answer, 1 is accepted

Sort by
0
Andrey
Telerik team
answered on 09 Jan 2012, 04:39 PM
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.

Greetings,
Andrey
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
Tags
Ajax
Asked by
Pirooz
Top achievements
Rank 1
Answers by
Andrey
Telerik team
Share this question
or