Persist table's controls

  • Thread starter Thread starter Nhat Yen
  • Start date Start date
N

Nhat Yen

Hi everyone,

I don't know why all the controls of the Table class (server control)
has to be reconstructed for each page load. The MSDN said that it is
because the children controls are not the Table properties. But why
other controls like ListBox or DataGrid can persist their child
controls?

I'm very confused about this.

Thanks for any reply,

Nhat Yen
 
Hi,

all non-controls are persisted via ViewState and they can be restored that
way. But controls do need to recreated on every request (and they then load
their own state indepedently after they have been created)

ListBox works so that it saves the state by calling SaveViewState of its
Items collection (ListItemCollection) which stores the items in key/value
sense. On postback items are loaded from ViewState and ListItemCollection
is reconstructed. E.g ListBox actually uses the similar procedure to
repopulate the collection. ListItems aren't controls so therefore ListBox
can work this way without controls "in the middle".

DataGrid can recreate the controls because they (child controls) are usually
specified in templates (or via columns). DataGrid stores the count of rows
which it uses to recreate the Items collection (by instantiating templates)
and then the child controls load their state independently after they've
been created. So basically DataGrid doesn't store the child controls but
recreates them on postback.

Basically you can't avoid recreating controls because control are themselves
responsible for storing their state. Also it would be terribly inefficient
tho store complete control instances to ViewState, therefore only control
state is stored and controls themselves are recreated.
 
Back
Top