I need 3 panels. Instead of manually name this panels I want an array of panels? how can I do this in VB.net and ASP
in my ASP page I have
<asp:Panel ID="Panel1" runat="server"></asp:Panel>
Then in VB.net I generate an array of panels. which will be added to Panel1. This is what I have so far (inside a loop)
Dim DivPanel() As Panel = Nothing
For i = 0 To 2
DivPanel(i) = new panel
DivPanel(i).controls.add(txtBox)
Panel1.controls.add(DivPanel(i))
Next
I got an error at "DivPanel(i) = new panel" . The error is "NullReferenceException was caught"
please advice... thank you thank you very much
You get the error because you've declared the array but you haven't initialized it here:
Dim DivPanel() As Panel = Nothing
However, you don't need that array at all:
For i = 0 To 2
Dim panel = new Panel()
Dim txtBox = new TextBox()
panel.controls.add(txtBox)
Panel1.controls.add(panel)
Next
Note that i have also created one TextBox
for every panel, you cannot use the same instance.
Remember to recreate dynamically created control on every postback with the same ID as before in Page_Load
at the latest. Threfore you need to know how many controls were already created. You could use the ViewState
to persist the number.
Recommandable readings:
Or you use one of the builtin Data-Bound Control like Repeater that do this automatically. You only have to set their DataSource
and call DataBind
().
Here are answers of me on similar questions with implementations:
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.