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

Dynamic Load of User Control Causes Crazy Viewstate Problem

2 Answers 336 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Sunil
Top achievements
Rank 1
Sunil asked on 24 Aug 2010, 03:34 PM
Hi, I have a really weird problem.

 I've an aspx page which dynamically loads web user controls into a panel. It loads about 10 user controls, 1 at a time. However, only 3 are causing issues.

FYI, I'm loading the user controls in the onint event as below:

protected override void OnInit(EventArgs e)
        {
            //If we're loading for the first time load default page.
            if (Session["wuc_location"] == null)
           {
               Session["wuc_location"] = "default_wuc.ascx";
               Session["wuc_ID"] = "WUC_default_wuc";
           }
           else
           {
               //Choose which WUC to load based on button clicked!
               switch ((string)Request.Form["__EVENTTARGET"])
               {
                   case "LinkButton_MM_Inbox":
                       Session["wuc_location"] = "administration_wuc.ascx";
                       Session["wuc_ID"] = "WUC_administration_wuc";
                       Session["Page_First_Load"] = true;
                       break;
                   case "LinkButton_MM_System_Logs":
                       Session["wuc_location"] = "logs_main_wuc.ascx";
                       Session["wuc_ID"] = "WUC_logs_main_wuc";
                       Session["Page_First_Load"] = true;
                       break;
                   case "LinkButton_MM_Preferences":
                       Session["wuc_location"] = "user_preferences_wuc.ascx";
                       Session["wuc_ID"] = "WUC_user_preferences_wuc";
                       Session["Page_First_Load"] = true;
                       break;
                   case "LinkButton_P20T":
                       Session["wuc_location"] = "previous_20_transactions_wuc.ascx";
                       Session["wuc_ID"] = "WUC_previous_20_transactions_wuc";
                       Session["Page_First_Load"] = true;
                       break;
                   case "WUC_administration_wuc$LinkButton_ADMIN_SMS_Settings":
                       Session["wuc_location"] = "sms_settings_wuc.ascx";
                       Session["wuc_ID"] = "WUC_sms_settings_wuc";
                       Session["Page_First_Load"] = true;
                       break;
                   case "WUC_administration_wuc$LinkButton_ADMIN_Department_Setup":
                       Session["wuc_location"] = "department_setup_wuc.ascx";
                       Session["wuc_ID"] = "WUC_department_setup_wuc";
                       Session["Page_First_Load"] = true;
                       break;
                   case "WUC_administration_wuc$LinkButton_ADMIN_Terminal_Setup":
                       Session["wuc_location"] = "terminal_setup_wuc.ascx";
                       Session["wuc_ID"] = "WUC_terminal_setup_wuc";
                       Session["Page_First_Load"] = true;
                       break;
                     
                   default:
                       //
                       break;
               }
           }
  
            //Load the selected user control
            Main_PH.Controls.Clear();
            WUC_default_wuc = new Control();
            WUC_default_wuc = this.LoadControl((string)Session["wuc_location"]);
            WUC_default_wuc.ID = (string)Session["wuc_ID"];
            Main_PH.Controls.Add(WUC_default_wuc);
            base.OnInit(e);
        }

I've deleted a lot of "cases" in the switch above to reduce the size of the code.

So below is one of the problems I'm having:

The aspx page has a menu on the left with links such as ADMINISTRATION, SYSTEM LOGS, USER PREFERENCES - If I click on Administration, the wuc_administration.ascx is loaded into Main_PH. I could have another 10 links from within administration.ascx - 1 in particular is the SMS settings page. Clicking on SMS settings loads:

case "WUC_administration_wuc$LinkButton_ADMIN_SMS_Settings":
                       Session["wuc_location"] = "sms_settings_wuc.ascx";
                       Session["wuc_ID"] = "WUC_sms_settings_wuc";
                       Session["Page_First_Load"] = true;
                       break;

Now if I click on the USER PREFERENCES link on the left menu which loads:

case "LinkButton_MM_Preferences":
                       Session["wuc_location"] = "user_preferences_wuc.ascx";
                       Session["wuc_ID"] = "WUC_user_preferences_wuc";
                       Session["Page_First_Load"] = true;
                       break;

I get a view state error:

Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: 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.

Its driving me insane. I understand Viewstate and the way it works... but I don't see how I could be getting viewstate errors when a user control is removed and a new one is added? This error only occurs when traversing from certain user controls. I could go to the email settings control and then to the user preferences control with no problem.....

To make things more interesting I did some debugging.... check out the pictures.

1. - A screen of sms settings (sms.jpg)
2. - A screen of user preferences (user_pref.jpg)
3. - A screen of user preferences after I swap the 2nd dropdown for a label. (user_pref1.jpg)

Screen 3 is the most important. After the user_preferences_wuc.ascx control kept throwing the viewstate error when you click to it from sms_settings_wuc.ascx, I got rid of the 2nd dropdown from user preferences and replaced it with a label:

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>


When I did this, the result was screen 3. There was no viewstate error but the label got populated with the viewstate from the previous sms_settings_wuc.ascx control..... How is this possible?????? Can somebody shed some light on what is happening??

2 Answers, 1 is accepted

Sort by
0
Accepted
Rosen
Telerik team
answered on 26 Aug 2010, 04:26 PM
Hi Sunil,

The reason for the error you are getting is due to incorrect loading of the UserControls. As you are switching UserControls before the ViewState is loaded, ASP.NET will try to load the ViewState from the previously loaded user control into the new one. Thus, as the controls collection of the two does not match, an exception is thrown. 
In order to correct this you should load the new UserControls inside the button's click handler instead and use Page's Init event to load the same UserControl which has been loaded in the previous page request.

Best wishes,
Rosen
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
Sunil
Top achievements
Rank 1
answered on 26 Aug 2010, 05:00 PM
That solved it :)

Thank you very much!!
Tags
Ajax
Asked by
Sunil
Top achievements
Rank 1
Answers by
Rosen
Telerik team
Sunil
Top achievements
Rank 1
Share this question
or