Composite Control sequencing question

Discussion in 'ASP .Net Web Controls' started by Cathead, Sep 11, 2003.

  1. Cathead

    Cathead Guest

    I have a simple composite control (below) that renders a list of
    words/phrases. I am adding words to the control within the Page_Load()
    method of the containing page's code-behind (also below).

    The control renders my word list fine for the initial request, but not on
    postback. On postback, no words are displayed. I've come to realize that
    CreateChildControls() is invoked earlier during a postback: before
    Page_Load(), when there are not yet any words to display.

    So obviously Page_Load is a bad place to add words to my composite control.
    I've moved the code that adds words to the control to OnInit(), and it works
    fine there, since it always runs before CreateChildControls() runs.

    Given all of the above, my question is: When using a composite control that
    overrides CreateChildControls() to render itself, is it standard procedure
    to set up the control (i.e. add words) during the Init phase of the cycle
    versus the other phases? Or is there another way?

    It sounds like I'm answering my own question here, but I'm a newbie to
    ASP.NET and am curious about what "best practices" are with regard to server
    controls.

    Regards,

    CH

    -----------------------
    | The composite control |
    -----------------------

    public class TestControl : WebControl, INamingContainer
    {
    ArrayList _testList = new ArrayList();

    /// <summary>
    /// Adds a word or phrase to _textList
    /// </summary>
    public void AddText(string text)
    {
    _textList.Add(text);
    }


    /// <summary>
    /// Overridden a la Composite control technique.
    /// </summary>
    protected override void CreateChildControls()
    {
    base.CreateChildControls();

    Table table = new Table();
    TableRow tr = null;

    // Add each word/phrase within a new table row
    foreach(string text in _textList)
    {
    tr = new TableRow();
    table.Rows.Add(tr);
    cell = new TableCell();
    cell.Controls.Add(new LiteralControl(text));
    tr.Cells.Add(cell);
    }

    this.Controls.Add(table);
    }
    }


    ------------------
    | Code-behind page |
    ------------------

    public class Test : Page
    {
    private void Page_Load(object sender, System.EventArgs e)
    {
    _testControl.AddText("Hello");
    _testControl.AddText("World");
    }
    }
    Cathead, Sep 11, 2003
    #1
    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. Kamal Jeet Singh
    Replies:
    1
    Views:
    702
    Scott Allen
    Sep 23, 2004
  2. Kamal Jeet Singh
    Replies:
    1
    Views:
    439
    Martin Dechev
    Sep 24, 2004
  3. Tom Houston via .NET 247

    Web form sequencing questions - vb.net & tab order

    Tom Houston via .NET 247, Mar 13, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    727
    Tom Houston via .NET 247
    Mar 13, 2005
  4. Mar Thomas

    newbie question on sequencing

    Mar Thomas, Aug 26, 2003, in forum: Java
    Replies:
    2
    Views:
    359
    Brad BARCLAY
    Aug 27, 2003
  5. Chad
    Replies:
    0
    Views:
    209
Loading...

Share This Page