Consider I have 2 controls, C and CompositeC which contains C. Both C and CompositeC have ViewState.
Now consider a third control causes a postback.
The resulting life cycle will be as follow:
CompositeCtries to restore state. To do so it needs to create the child controls, so it createsC.- When
Cis created it’s ViewState is restored. - The third control’s postback event is triggered. At this time a handler calls
CompositeC.RecreatedChildControls()andCis created again. - Everything renders.
The problem is that C state is not restored again after step 3. RecreateChildControls method should modify the control’s state so that when C is reintroduced to the CompositeC control’s collection it’s viewstate is automatically restored but it doesn’t.
What should I do?
Actually I found this is by design and it makes sense. ViewState is meant to allow for state restoration after a postback. If after that I recreate my controls, it won’t take place.