questions about Controls.add

Discussion in 'ASP .Net' started by Bob, Jun 16, 2006.

  1. Bob

    Bob Guest


    In aspx file, i have a table:
    <asp:Table ID="table1" runat="server">

    In the code-behind, i create rows and cells:
    For i = 0 To 50
    r = New TableRow()
    for j = 0 To 20
    c(i, j) = New TableCell()
    r.Cells.Add(c(i, j))

    With this, the table is rendered perfectly (data are displayed in all rows
    and cells).

    Now i added this line just after 'r.Cells.Add(c(i, j))':
    Controls.Add(c(i, j))

    Now i have no table anymore: data are dispayed the one after the other in
    the page.

    My questions are:
    1) where is my table, why is is gone?
    2) what's the difference between 'Controls.Add(c(i,j))' and
    r.Cells.Add(c(i, j)) in this case?

    Thanks for help
    Bob, Jun 16, 2006
    1. Advertisements

  2. In one case you are adding row and cell objects to a table. So it
    appropriately renders them

    In the other case you are adding them to the page - not to the table. So
    the page ends up with these objects, which it just renders in the order they
    were added. Unlike the table which actually knows how to deal with these,
    the page just sort of throws them on one after the other.

    It's the difference between adding an object to a Panel you have on your
    page, or to the page itself. They are different containers for your objects.
    Marina Levit [MVP], Jun 16, 2006
    1. Advertisements

  3. Bob

    Bob Guest

    Thanks for your explanation.
    I have just sent another problem, related with this, but first i had to know
    this answer.
    Bob, Jun 16, 2006
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.