简体   繁体   中英

Add panel and controls dynamically

I am working on hotel booking web site, need to write all active bookings in panels with buttons and labels in each of them This piece of code works properly

'creo un pannello per ogni pratica
Dim p1 As New Panel
topd = 270 + 220 * x  ' parto da 270 poi altezza 200 e 20 px di distanza con pannello successivo
p1.Attributes.Item("style") = "top:" & topd & "px; left:50px; z-index: 1; position: absolute; height: 200px; width: 600px;runat: server"
p1.BorderColor = Drawing.Color.Silver
p1.BorderWidth = Unit.Pixel(1)
p1.Width = Unit.Pixel(600)
p1.Height = Unit.Pixel(200)
p1.ID = "P_" & pratica
Me.Controls.Add(p1)
'nel pannello metto la label di descrizione
Dim l1 As New Label
l1.Attributes.Item("style") = "Z-INDEX: 102; POSITION: absolute; TOP: 10px; LEFT: 10px; height: 150px; width:250px; Font-Size:9pt; text-align:center"
l1.BorderStyle = BorderStyle.None
l1.BackColor = Drawing.Color.White
l1.Text = rosso & testo(22) & sp & npratica & SPANFINE & sr
l1.Text += "Hotel " & preno(1) & sr & testo(24) & sp & preno(4) & sr & testo(25) & sp & preno(5) & sr
l1.Text += testo(28) & sp & preno(10) & sr & testo(26) & sp & preno(6) & sr & testo(27) & sp & preno(7) & sr
l1.Text += testo(29) & sp & preno(11) & sr
l1.ForeColor = Drawing.Color.Blue
p1.Controls.Add(l1)

This works

Now for test I want to add a button and I add the following

b1.Text = "prova"
p1.Controls.Add(b1)

I get this error

Control 'ctl01' of type 'Button' must be placed inside a form tag with runat=server.

If I cancel label and add button only I get the same error. If I put panel in html (don't create it dynamically) it works.

Ops I found it sorry.

Me.Controls.Add(p1) 

is wrong I have to add to form1

form1.Controls.Add(p1)

I think I should keep this on stack, maybe can help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM