Dynamicly create table once

T

Tony

win xp - vs 2002 - vb - web forms

I have this code in the page_load event.
works fine except, I put it all in an if not ispostback then.
I don't want to run all this code (will get much bigger) each round trip.
The table disappears on each round trip, unless I leave it out of the not
isposback.
Anyway to create once or do I have to run this code each round trip?

Thanks Tony

table.Visible = True
table.BackColor = Color.Ivory
table.GridLines = GridLines.Both
table.Visible = True

For rowloop = 0 To 5
Dim trow As New TableRow()
For cellsloop = 0 To 2
Dim tcell As New TableCell()
trow.Cells.Add(tcell)
Next cellsloop
table.Rows.Add(trow)
table.Rows(rowloop).Cells(0).VerticalAlign =
VerticalAlign.Middle
table.Rows(rowloop).Cells(0).Width() = Unit.Pixel(200)
table.Rows(rowloop).Cells(1).Width() = Unit.Pixel(200)
table.Rows(rowloop).Height = Unit.Percentage(100)
table.Rows(rowloop).Cells(0).Wrap = True
Next rowloop

labFirstName.Text = "Enter Frist Name"
table.Rows(0).Cells(0).Controls.Add(labFirstName)

labLastName.Text = "Enter Last Name"
table.Rows(1).Cells(0).Controls.Add(labLastName)

labEMail.Text = "Enter E-Mail Address"
table.Rows(2).Cells(0).Controls.Add(labEMail)

table.Rows(0).Cells(1).Controls.Add(txtFirstName)
table.Rows(1).Cells(1).Controls.Add(txtLastName)
table.Rows(2).Cells(1).Controls.Add(txtEMail)

Page.Controls(1).Controls.Add(table)


table.Style.Add("10", "Z-INDEX: 101; LEFT: 150px; POSITION:
absolute; TOP: 150px")
 
W

Wilco Bauwer

After each postback (or roundtrip if you like), the control hierarchy
is created "from scratch". If you add a control to the Controls
collection, you do this for the _current request only_. After the next
roundtrip you have to READD this control again. So when dealing with
dynamically created controls, you should NOT check if the request is a
postback. Such checks should generally be done ONLY to avoid
unnecessary roundtrips to a resource such as a DB, when the data is
already in place (due to the viewstate).

In your case you probably want to create a composite control and put
the control creation in CreateChildControls.
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top