Re: postback и динамические контролы
От: yaroslav.ov Россия  
Дата: 24.03.06 17:05
Оценка:
L>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.

У меня такая ошибка возникала когда один контрол (статический — постоянно объявленный на странице) загружал динамически другие контролы в зависимости от своих свойств.
Чтобы ошибка не возникала надо было "перевести" загрузку view state из режима загрузки по индексу в режим загрузки по ID. Для этого необходимо установить атрибут [ViewStateModeById] в объявлении класса Parent-контрола. И естественно при этом обеспечить уникальность ID для динамически загружаемых контролов. Теперь Parent-контрол для всех child controls view state будет восстанавливать по ID, а не по индексу.

Вот тут можно почитать по этому поводу

Ну или привожу такой вот кусок:

By default, when a parent control loads view state into child controls it creates, it does this by the position of each child control in the parent control's Controls collection. When view state is initially applied, all child controls might not have been created. In this case, the view state for controls not yet created is saved, and applied when the child controls are created later.

For a parent control to apply view state to its child controls, two conditions must be met:

On postback, the parent control must create the child controls in exactly the same order as the previous request so that the order of the controls remains consistent.

After postback, any child controls created must be added to the end of the parent control's Controls collection.

If these two conditions cannot be met, as in the case of the delayed creation of a child control, the parent control can load view state by using ID. To set the LoadViewStateByID property to true, use the ViewStateModeByIdAttribute metadata attribute for the parent control.

 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.