I am trying to dynamically create a new textbox whenever a button is clicked. The page requirements are for a user to enter information in as many textboxes as they may need, so I can't just throw a set number of PlaceHolders onto the page, and then dynamically add controls to it.
So far, I am able to add controls to a placeholder already on the page, but when I click my "Add Another" button, it just replaces that control with a new one, instead of adding it below it.
I've tried various ways of approaching this, and this is currently where I'm at (in VB):
'This is to be used in duplicating the PLO textbox within the panel
Dim newTextboxPLO As New TextBox() 'make a new textbox
Dim newLabelPLO As New Label() 'make a new label
Dim plhNewPlaceHolder As New PlaceHolder() 'make a new place holder
'Add a spacer
Dim spacer As LiteralControl = New LiteralControl("<br />")
ploCounter += 1
'New place holder attributes
plhNewPlaceHolder.ID = "plhNewPlaceHolder" & ploCounter
'New textbox attributes
newTextboxPLO.ID = "txtPLO" & ploCounter
newTextboxPLO.TextMode = TextBoxMode.MultiLine
newTextboxPLO.Width = "318"
newTextboxPLO.Height = "70"
'New label attributes
newLabelPLO.Text = (ploCounter) & ") "
newLabelPLO.Font.Bold = True
newLabelPLO.ID = "lblPLO " & (ploCounter)
pnlAddPLO.Controls.Add(plhNewPlaceHolder)
'If the original place holder has content in it, add the new textbox and label into a new placeholder
If plhPLO.HasControls = False Then
'Add new textbox and label into the placeholder
plhPLO.Controls.Add(newLabelPLO)
plhPLO.Controls.Add(newTextboxPLO)
plhPLO.Controls.Add(spacer)
Else
'Add new placeholder into the panel
pnlAddPLO.Controls.Add(plhNewPlaceHolder)
'Add items into the new placeholder
plhNewPlaceHolder.Controls.Add(newLabelPLO)
plhNewPlaceHolder.Controls.Add(newTextboxPLO)
plhNewPlaceHolder.Controls.Add(spacer)
'Increase the place holder counter
plhCounter += 1
End If
I already checked out many questions, including this one, but it didn't quite help. I feel like there's something small that I'm missing, but I'm not sure what.