0

In my ASP.NET page I have to dynamically choose and load a custom control, depending on the selected value in a dropdownlist.However I encountered the following problem: When the parameters of the dynamically loaded control are changed, and then the selection in the dropdownlist is changed( thus forcing me to load a different dynamic control the next time the page reloads ), I end up with a "Cannot load ViewState" exception.I assume that this happens because the ViewState is trying to restore the parameters of the old control and it doesn't find it. So , is there any way to stop the viewstate from attempting to restore the state of the non-existig control?

Emil D
  • 1,864
  • 4
  • 23
  • 40

4 Answers4

1

You should load the controls the exact same way initially and then alter then after LoadViewState or disable the viewstate on the dynamic controls you know will not be in sync with the page.

Glennular
  • 17,827
  • 9
  • 58
  • 77
0

It sounds like the state of the drop down / added control is not being restored before you are restoring the view state. If you have the drop down defaulted to show control X, and the user changes it to show control Y, the page must add control Y to the control collection before view state is restored.

Tejs
  • 40,736
  • 10
  • 68
  • 86
  • That's exactly what I'm doing, and I believe that's what's causing the problem: control Y is added before the viewstate is restored, but control X is not, bacause it is no longer needed.However, the viewstate tries to restore control X's state , but it doens't find that control – Emil D Apr 28 '10 at 15:31
  • Hrm, that's a tough question then. It would be hard to speculate without seeing the full code and trying to run some test runs tinkering with the life cycle. Have you tried just leaving X in and then removing it after the view state is restored? – Tejs Apr 28 '10 at 15:48
  • Well, I guess that's what I'll end up doing, but it's an ugly solution: you can't just leave a dynamic control in there, since it has to be reloaded at every postback.I'd essentially be loading a new control , just to remove it right afterwards:S – Emil D Apr 28 '10 at 17:55
0

I've the same issue with grid control. I was binding dataviews dynamically and according to DarrenMB's solution I've just write EnableViewState = false; and problem solved.

Infragistics.Web.UI.DataSourceControls.DataView dvMesaj = new Infragistics.Web.UI.DataSourceControls.DataView();


        whdsShowMessages.DataRelations.Clear();
        whdsShowMessages.DataViews.Clear();
        whgridShowMessages.Rows.Clear();
        EnableViewState = false; //here is the solution..
        whdsShowMessages.DataViews.Add(dvKisi);
        whdsShowMessages.DataViews.Add(dvMesaj);
0

Had the same issue where a variable length list of controls was added, rearranged and/or modified by the user and is changable during each postback.

The answer is surprisingly simple.

When you create the dynamic control set "EnableViewState = False" before you add it to the pages control collection. Then no viewstate information is stored and regardless of how many dynamic controls are added or removed or re-ordered the viewstate for everything else will work correctly.

If your adding these dynamically you are normally setting all the properties anyways so it didn't actually create any work in my case which is very similar.

DarrenMB
  • 2,342
  • 1
  • 21
  • 26
  • Just realized this is what Glennular answered but the text he left is not very clear, so I will leave this in place for others. – DarrenMB Jan 21 '12 at 00:23