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

Discussion in 'ASP .Net Building Controls' started by Ralf Müller, Dec 10, 2004.

  1. Ralf Müller

    Ralf Müller Guest

    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
    Ralf Müller, Dec 10, 2004
    #1
    1. Advertising

  2. Ralf Müller wrote:

    > 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
    Nicole Schenk, Dec 12, 2004
    #2
    1. Advertising

  3. John Saunders, Dec 12, 2004
    #3
  4. Ralf Müller

    Ralf Müller Guest

    I'm afraid John is right...

    "Nicole Schenk" <> schrieb im Newsbeitrag
    news:n7Rud.2193$...
    > Ralf Mller wrote:
    >
    > > 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
    >
    Ralf Müller, Dec 13, 2004
    #4
  5. Ralf Müller

    Ralf Müller Guest

    Here's the correct URL if anyone else is interested:
    http://www.dotnet247.com/247reference/a.aspx?u=http://aspalliance.com/359

    Greetings, Ralf


    "Ralf Müller" <> schrieb im Newsbeitrag
    news:OGNu#$...
    > I'm afraid John is right...
    >
    > "Nicole Schenk" <> schrieb im Newsbeitrag
    > news:n7Rud.2193$...
    > > Ralf Mller wrote:
    > >
    > > > 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
    > >

    >
    >
    Ralf Müller, Dec 13, 2004
    #5
  6. Nicole Schenk, Dec 15, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sleigh
    Replies:
    1
    Views:
    2,660
    sleigh
    Feb 12, 2004
  2. Harry
    Replies:
    2
    Views:
    646
    Harry
    Apr 16, 2004
  3. Jeff Rodriguez
    Replies:
    23
    Views:
    1,071
    David Schwartz
    Dec 9, 2003
  4. Chad
    Replies:
    0
    Views:
    207
  5. David Boike

    Child controls in Composite Controls not firing events

    David Boike, Aug 7, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    301
    David Boike
    Aug 7, 2003
Loading...

Share This Page