composite control: how to assign initial values to child controls?

R

Ralf Müller

Hi all,

I've got a question concerning composite controls:
As you can see, in my CreateChildControls method I have two custom child
controls, each of them using a model. This model should be assigned to those
controls only once in their lifetime since they keep it in their viewstate.
But how am I supposed to achieve this when CreateChildControls is strangely
invoked on each postback and therefore the initial values are reassigned
overwriting the values saved in ViewState (although I am not touching
ChildControlsCreated and my code in the parent control is not containing any
call to EnsureChildControls() - the code in the child controls does contain
those calls but they should only affect controls further down the tree,
shouldn't they?)?
Where should I assign initial values to my child controls since every method
apart from CreateChildControls which is called randomly seems to be called
at least once per postback?

protected override void CreateChildControls() {
Controls.Clear();

tvNavigationTree = new TreeView();
tvNavigationTree.Model = new NavigationTreeModel(Pool.GetHome());

lbDocumentList = new ListBox();
lbDocumentList.Model = new DocumentListModel();

TableRow row;
TableCell cell;
tblLayout = new Table();

row = new TableRow();
cell = new TableCell();
cell.Controls.Add(tvNavigationTree);
row.Cells.Add(cell);
cell = new TableCell();
cell.Controls.Add(lbDocumentList);
row.Cells.Add(cell);
tblLayout.Rows.Add(row);

Controls.Add(tblLayout);
}


Greetings, Ralf
 
N

Nicole Schenk

Ralf said:
Hi all,

I've got a question concerning composite controls:
As you can see, in my CreateChildControls method I have two custom child
controls, each of them using a model. This model should be assigned to
those controls only once in their lifetime since they keep it in their
viewstate. But how am I supposed to achieve this when CreateChildControls
is strangely invoked on each postback and therefore the initial values are
reassigned overwriting the values saved in ViewState (although I am not
touching ChildControlsCreated and my code in the parent control is not
containing any call to EnsureChildControls() - the code in the child
controls does contain those calls but they should only affect controls
further down the tree, shouldn't they?)?
Where should I assign initial values to my child controls since every
method apart from CreateChildControls which is called randomly seems to be
called at least once per postback?

protected override void CreateChildControls() {
Controls.Clear();

tvNavigationTree = new TreeView();
tvNavigationTree.Model = new NavigationTreeModel(Pool.GetHome());

lbDocumentList = new ListBox();
lbDocumentList.Model = new DocumentListModel();

TableRow row;
TableCell cell;
tblLayout = new Table();

row = new TableRow();
cell = new TableCell();
cell.Controls.Add(tvNavigationTree);
row.Cells.Add(cell);
cell = new TableCell();
cell.Controls.Add(lbDocumentList);
row.Cells.Add(cell);
tblLayout.Rows.Add(row);

Controls.Add(tblLayout);
}


Greetings, Ralf
Try
http://www.dotnet247/com/247/reference/a.aspx?u=http://aspalliance.com/359
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top