Page throws ViewState error when Items collection of dropdownlist is modified in IndexChanged event
I have an ASCX control which is a special dropdownlist. I add that control dynamically to the page and fill it with data. This control has a postback that will change the contents of a second dynamically created standard dropdownlist.
When I change the selection on the first dropdown, the indexchanged fires and I get new data and attempt to place it in the second dropdownlist's items collection, by first clearing it then filling it with new da开发者_如何学JAVAta.
This works fine the first time a change the selection, but when I select a second time the following error is thrown:
The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request.
I'm not adding or removing new controls in the fired event, only changing data. And again, it works the first time, but doesn't subsequent times.
If I disable stateview on the child control, then the control just doesn't get updated with data at all.
Hope this is clear enough :)
Thanks!
In your ASCX - are you re-creating both Dropdown lists each time in the control's OnInit event? If they are created dynamically, they need to be created each time there is a postback and this must be done before ViewState's values are deserialized in to the control tree.
精彩评论