Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate

I'm currently working on a dynamic core for several webprojects. It has a core that uses a treeview and a menu. And then for each specific projekt it loads several different wuc into a maincontent. Some business projects use business related wucs while others uses different ones. So the span of wuc's is really big.

Now to my problem, whenever a user press a menuitem or a treeitem it loads a wuc to the maincontent linked to that object.

But I'm having some viewstate errors and i've been looking around for 2 days now and none of the solutions explained are working for my projekt.

All my wuc has to have viewstate enabled.

Cycle is ->

Page(Control A) does postback with variable to change control to ControlB in wucPanel(UpdatePanel). OnLoad LoadRequested Wuc.

Current code is

protected void Load_Page(object sender, EventArgs e)
{
//Code to decide which wuc to load.
 UserControl wucc = (UserControl)Page.LoadControl(sFilePath);        
 ParentControl.ContentTemplateContainer.Controls.Add(wucc);
}

I've tried several fixes like adding diffrent ids to the wuc, but this either disabels the internal functions of control like handlers etc or generates the same viewstate error.

One solution i found was to load ControlA and then just removing it and then load ControlB. But this disabled the scripts for my 3rd party controller (Telerik).

I've also read about having diffrent PlaceHolders for each typof but since i expect havign up to 50 diffrent Controls I don't feel this is gonna help me.

And moving from Page_Load -> Page_Init generated the same error.

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.

like image 366
Anders Avatar asked Mar 02 '11 09:03

Anders


1 Answers

In your case Anders, you still need to add the old control to your page in the init method along with the new control that you now want to add. Keep a reference to this old control that you have just added in a class level variable. So something like

    Control _oldControl = null;
    protected void Init_Page(object sender, EventArgs e)
    {
    //Code to decide which wuc to load.
     UserControl wucc = (UserControl)Page.LoadControl(sFilePath);        
     ParentControl.ContentTemplateContainer.Controls.Add(wucc);
     _oldControl = wucc as Control;
    //Now add the new control here.
    }

   //override the LoadViewState method and remove the control from the control's collection     once you page's viewstate has been loaded 
    protected override void LoadViewState(object savedState)
    {
            base.LoadViewState(savedState);
            ParentControl.ContentTemplateContainer.Controls.Remove(_oldControl);
    }

Hope this helps. If it did, please check the checkbox next to this answer to accept it and vote it up if you like :)

like image 102
Nikhil Avatar answered Oct 06 '22 04:10

Nikhil