questions about Controls.add

B

Bob

Hi,

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

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))
Next
Table1.Rows.Add(r)
Next
....

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
 
M

Marina Levit [MVP]

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.
 
B

Bob

Thanks for your explanation.
I have just sent another problem, related with this, but first i had to know
this answer.
 

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,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top