Dynamic Load of User Control Causes Crazy Viewstate Problem

3 posts, 1 answers
  1. Sunil
    Sunil avatar
    29 posts
    Member since:
    Jul 2010

    Posted 24 Aug 2010 Link to this post

    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. Answer
    Rosen
    Admin
    Rosen avatar
    3253 posts

    Posted 26 Aug 2010 Link to this post

    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
  3. Sunil
    Sunil avatar
    29 posts
    Member since:
    Jul 2010

    Posted 26 Aug 2010 Link to this post

    That solved it :)

    Thank you very much!!
Back to Top