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

Error Loading ViewState - dynamic window creation and ajaxpanels

3 Answers 113 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 2
Kevin asked on 24 Feb 2009, 06:05 PM
I have a situation where we are getting an error loading viewstate.  The setup (code below) is that we have a page, using a master page.  On the master page we have a radwindowmanager.  That master page plugs 2 windows into its list that are common to all pages.  Inside our specific content pages, we have dynamic code that reads an xml file and uses that info to add additional windows to the radwindowmanager.  In this way, the master page handles making global windows available (like Help, Settings, etc.) and each content page adds whatever windows it needs to that.

Anyway, in those same content pages we also have ajaxpanels containing things like calendar controls, grids, and things like that.  What I've found is that after adding all the dynamic window code, using the controls in the ajaxpanel (like going to the next month on a calendar) causes the viewstate loading error.

What's the best way to correct this problem and also follow good design for all pages that do this?

Here's the .cs for the content page that shows the windows being built dynamically.  The relevant stuff is the SetupWindows method:

 

using System;  
using System.Data;  
using System.Configuration;  
using System.Collections;  
using System.Web;  
using System.Web.Security;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using System.Web.UI.WebControls.WebParts;  
using System.Web.UI.HtmlControls;  
using Sample.Schedule.Views;  
using Microsoft.Practices.ObjectBuilder;  
using Telerik.Web.UI;  
using Sample.CommonShared;  
using Sample.Messaging.Interface;  
 
namespace Sample.Schedule.Views  
{  
    public partial class ScheduleDefault : Microsoft.Practices.CompositeWeb.Web.UI.Page, IDefaultView  
    {  
        private DefaultViewPresenter _presenter;  
 
        protected void Page_Load(object sender, EventArgs e)  
        {  
            if (!this.IsPostBack)  
            {  
                this._presenter.OnViewInitialized();  
                SetupWindows();  
            }  
            this._presenter.OnViewLoaded();  
        }  
 
        [CreateNew]  
        public DefaultViewPresenter Presenter  
        {  
            get 
            {  
                return this._presenter;  
            }  
            set 
            {  
                if (value == null)  
                    throw new ArgumentNullException("value");  
 
                this._presenter = value;  
                this._presenter.View = this;  
            }  
        }  
 
 
        private void SetupWindows()  
        {  
            RadWindowManager mgr = this.Master.FindControl("RadWindowManagerMaster"as RadWindowManager;  
            if (null == mgr)  
                throw new Exception("Contacts : RadWindowManager not found");  
 
            TypedDialog tds = new TypedDialog();  
            tds.ReadXml(Server.MapPath(this._presenter.msg.GetMessage("schedule_dialogxml")));  
 
            RadWindow thewin = new RadWindow();  
 
            for (int i = 0; i < tds.window.Count; i++)  
            {  
                thewin.NavigateUrl = tds.window[i].navigateurl;  
                thewin.ID = tds.window[i].id;  
                thewin.VisibleOnPageLoad = false;  
                thewin.Width = tds.window[i].width;  
                thewin.Height = tds.window[i].height;  
                thewin.EnableEmbeddedSkins = (tds.window[i].enableembeddedskins == "true");  
                thewin.Skin = tds.window[i].skin;  
                if (string.Empty != tds.window[i].onclientclose) thewin.OnClientClose = tds.window[i].onclientclose;  
                if ("true" == tds.window[i].ismodal) thewin.Modal = true;  
                thewin.VisibleStatusbar = false;  
                mgr.Windows.Add(thewin);  
 
                thewin = new RadWindow();  
            }  
            thewin.Dispose();  
        }  
    }  
}  
 

 

...and here's the content page aspx file:

 

<%@ Page Language="C#" AutoEventWireup="true" Codebehind="Default.aspx.cs" Inherits="Sample.Schedule.Views.ScheduleDefault" 
    Title="Default" MasterPageFile="~/Shared/DefaultMaster.master" %> 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
<%@ Register src="../RecentDeployments.ascx" tagname="RecentDeployments" tagprefix="uc1" %> 
<%@ Register src="../ScheduleCalendar.ascx" tagname="ScheduleCalendar" tagprefix="uc2" %> 
<asp:Content ID="content" ContentPlaceHolderID="DefaultContent" Runat="Server">  
<link href="../Css/Schedule/Schedule.css" rel="stylesheet" type="text/css" /> 
<link href="../Css/UserControls/RecentDeployments.css" rel="stylesheet" type="text/css" /> 
<link href="../Css/UserControls/ScheduleCalendar.css" rel="stylesheet" type="text/css" /> 
 
<telerik:RadFormDecorator ID="RadFormDecorator1" Runat="server" Skin="Sample" EnableEmbeddedSkins="false" DecoratedControls="All" /> 
 
    <script type="text/javascript">  
        //<![CDATA[
        function showGetStarted() {
            var oM = GetRadWindowManager();
            var oW = oM.GetWindowByName("wndDeploy");
            oW.Show();
        }
        function showAddContacts() {
            var oM = GetRadWindowManager();
            var oW = oM.GetWindowByName("wndAddContacts");
            oW.Show();
        }
        function showUploadContacts() {
            var oM = GetRadWindowManager();
            var oW = oM.GetWindowByName("wndUploadContacts");
            oW.Show();
        }
        function showEditContacts() {
            var oM = GetRadWindowManager();
            var oW = oM.GetWindowByName("wndEditContacts");
            oW.Show();
        }
        function OnClientClose(radWindow) {
            //TODO window.location.href = window.location.href;
        }
        //]]> 
    </script> 
 
<div id="contentpage">  
<div class="schedule_fullwidth marginbottom-30">  
    <div class="schedule_leftpod">  
        <h3><asp:Label ID="lblDeploy" runat="server" Text="<%$ Resources:WebResources, dash_deployer_lbldeploy %>"/></h3> 
        <h4>Get started Now</h4> 
        <p> 
            <asp:Label ID="lblGetStartedNow" runat="server" Text="<%$ Resources:WebResources, dash_deployer_lblgetstartednow %>"/><br /> 
            <asp:Button ID="btnGetStarted" runat="server" OnClientClick="showGetStarted();return false;" Text="<%$ Resources:WebResources, button_getstarted %>" />   
        </p> 
        <div class="schedule_leftpod_data">  
            <h4><asp:Label ID="lblContacts" runat="server" Text="<%$ Resources:WebResources, dash_deployer_lblcontacts %>"/></h4> 
            <p> 
                <asp:Label ID="lblContactsCount" runat="server" Text="<%$ Resources:WebResources, dash_deployer_lblcontactscount %>"/><br /> 
                <asp:Label ID="lblGroupsCount" runat="server" Text="<%$ Resources:WebResources, dash_deployer_lblgroupscount %>"/>  
            </p> 
        </div> 
        <div class="schedule_leftpod_icons">  
            <img src="../Images/deployer-dashboard-contact_male.png" alt="deployer-dashboard-contact_male" width="40" height="50"/>  
            <img src="../Images/deployer-dashboard-contact_female.png" alt="deployer-dashboard-contact_female" width="40" height="50"/>  
        </div> 
        <div class="clear"></div> 
        <p> 
            <asp:Button ID="btnAddContacts" runat="server" OnClientClick="showAddContacts();return false;" Text="<%$ Resources:WebResources, button_addcontacts %>" /><br /> 
            <asp:Button ID="btnEditContacts" runat="server" OnClientClick="showEditContacts();return false;" Text="<%$ Resources:WebResources, button_edit %>" /><br /> 
            <asp:Button ID="btnUploadContacts" runat="server" OnClientClick="showUploadContacts();return false;" Text="<%$ Resources:WebResources, button_uploadcontacts %>" /><br /> 
        </p> 
    </div> 
    <div class="schedule_rightpod">  
        <div class="schedule_rightpod_title">  
            <h3>Schedule</h3> 
        </div> 
        <div class="schedule_rightpod_description">  
            <p>August Stats: 23 Campaigns</p> 
        </div> 
        <div class="clear"></div> 
        <div class="schedule_rightpod_headeritems">  
            <p><img src="../Images/deployer-dashboard-icon_brochure.png" alt="deployer-dashboard-icon_brochure" width="28" height="26"/><strong>BROCHURE<br /></strong>Brochures</p> 
        </div> 
        <div class="schedule_rightpod_headeritems">  
            <p><img src="../Images/deployer-dashboard-icon_email.png" alt="deployer-dashboard-icon_email" width="31" height="25"/><strong>EMAIL&nbsp;&nbsp;&nbsp;<br /></strong>Emails</p> 
        </div> 
        <div class="schedule_rightpod_headeritems">  
            <p><img src="../Images/deployer-dashboard-icon_ecard.png" alt="deployer-dashboard-icon_ecard" width="26" height="26"/><strong>ECARD&nbsp;&nbsp;<br /></strong>Emails</p> 
        </div> 
        <div class="schedule_rightpod_headeritems">  
            <p><img src="../Images/deployer-dashboard-icon_newsletter.png" alt="deployer-dashboard-icon_newsletter" width="24" height="27"/><strong>NEWSLETTER<br /></strong>Newsletters</p> 
        </div> 
        <div class="schedule_rightpod_headeritems">  
            <p><img src="../Images/deployer-dashboard-icon_coversplash.png" alt="deployer-dashboard-icon_coversplash" width="22" height="26"/><strong>COVERSPLASH<br /></strong>Surveys</p> 
        </div> 
        <div class="schedule_rightpod_headeritems">  
            <p><img src="../Images/deployer-dashboard-icon_form.png" alt="deployer-dashboard-icon_form" width="28" height="26"/><strong>FORM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /></strong>Documents</p> 
        </div> 
        <div class="clear"></div> 
        <div class="schedule_rightpod_calendar">  
        <telerik:RadAjaxPanel ID="ajaxpanelScheduleCalendar" runat="server" Height="375px" Width="660px" LoadingPanelID="ajaxloadingpanelScheduleCalendar">  
            <uc2:ScheduleCalendar ID="ScheduleCalendar1" runat="server" width="660" height="375" /> 
        </telerik:RadAjaxPanel> 
        </div> 
    </div> 
</div> 
<div class="schedule_fullwidth">  
</div> 
<telerik:RadAjaxLoadingPanel ID="ajaxloadingpanelScheduleCalendar" runat="server" Height="75px" 
    Width="75px">  
    <img alt="Loading..." src='<%= RadAjaxLoadingPanel.GetWebResourceUrl(Page, "Telerik.Web.UI.Skins.Default.Ajax.loading.gif") %>' 
        style="border: 0px;" /> 
</telerik:RadAjaxLoadingPanel> 
</asp:Content> 
 
 

We'll have many pages that take this approach of dynamic window instantiation, plugged into master page radwindowmanager, and multiple ajaxpanels on the content page doing "stuff."

Thx.

3 Answers, 1 is accepted

Sort by
0
Fiko
Telerik team
answered on 26 Feb 2009, 03:06 PM
Hello Kevin,

I was not able to reproduce the problem by using the provided code, but please note that some part of the code is missing ( e.g. all values that are in the XML document ). In this case I am not quite sure what exactly is the reason for this issue, but I suppose that you get an error like the described in the following forum threads :

If this information does not help, could you please open a new support ticket and send us a simple demo project that reproduce the problem?

Kind regards,
Fiko
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
Kevin
Top achievements
Rank 2
answered on 27 Feb 2009, 03:45 PM
I created a simple test program that stripped out all the other stuff going on.  It comes down to the fact that I'm building my RadWindows (in SetupWindows) only on a non-postback versus every page_load.  Guess the error makes sense... I create the window, but then do a grid ajax request (paging) and on postback I am not building the RadWindows again.

When I tweak things to build my RadWindows on every page_load I don't seem to have any issues.

I guess I was assuming that building the RadWindows I needed only on the first call to the page (the non-postback) would be more efficient and that calling it every time would be unnecessary.  Bad assumption?

Thx
0
Fiko
Telerik team
answered on 02 Mar 2009, 09:02 AM
Hello Kevin,

The purpose of the viewstate is to attempt to persist state for controls across postbacks. In some scenarios the viewstate, however, causes some problems when adding controls dynamically. For example, in your case you dynamically add a RadWindow to the page, but that RadWindow should exist before the viewstate for the page is loaded after a postback. This is because the when the page attempts to load the viewstate, the control hierarchy must match the control hierarchy that existed when viewstate was initially created.
I hope that information helps.

Best wishes,
Fiko
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.
Tags
General Discussions
Asked by
Kevin
Top achievements
Rank 2
Answers by
Fiko
Telerik team
Kevin
Top achievements
Rank 2
Share this question
or