0

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.

Community
  • 1
  • 1
Krizz
  • 3
  • 2

1 Answers1

0

The reason you have always one single PlaceHolder rendered on your page, is that an asp button click always triggers a page postback (refresh). Thus, your page is always going to have one single placeholder control, the one specified in your aspx file. What I suggest you to do, inspired by this post, is to store your placeholders in a list, store that list in a session variable and then load the stored placeholders on your page at load. Here is an example, considering the fact that you have no empty placeholders at page load:

Private lstPlaceHolders As New List(Of WebControls.PlaceHolder)

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If (Session("myPlaceholders") IsNot Nothing) Then
        Me.lstPlaceHolders = Session("myPlaceholders")
        ' Add your stored placeholders one by one
        For Each placeHolder As WebControls.PlaceHolder In lstPlaceHolders
            pnlAddPLO.Controls.Add(placeHolder)
        Next
    End If
End Sub

Protected Sub btnNewPlaceholder_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnNewPlaceholder.Click
    '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 WebControls.PlaceHolder() 'make a new place holder
    'Add a spacer
    Dim spacer As LiteralControl = New LiteralControl("<br />")

    ' Your placeholder counter always resets on every Postback, but the new placeholder's number will always be max + 1
    Dim newPlaceHolderNumber As Integer = Me.lstPlaceHolders.Count + 1

    'New place holder attributes
    plhNewPlaceHolder.ID = "plhNewPlaceHolder" & newPlaceHolderNumber
    'New textbox attributes
    newTextboxPLO.ID = "txtPLO" & newPlaceHolderNumber
    newTextboxPLO.TextMode = TextBoxMode.MultiLine
    newTextboxPLO.Width = "318"
    newTextboxPLO.Height = "70"

    'New label attributes
    newLabelPLO.Text = (newPlaceHolderNumber) & ") "
    newLabelPLO.Font.Bold = True
    newLabelPLO.ID = "lblPLO " & (newPlaceHolderNumber)

    'Add items into the new placeholder
    plhNewPlaceHolder.Controls.Add(newLabelPLO)
    plhNewPlaceHolder.Controls.Add(newTextboxPLO)
    plhNewPlaceHolder.Controls.Add(spacer)

    lstPlaceHolders.Add(plhNewPlaceHolder)

    ' Add the placeholder in your panel here too, since the load event is always triggered before your btnClick event on a Postback
    ' and you don't want to refresh the page to see your new placeholder.
    pnlAddPLO.Controls.Add(plhNewPlaceHolder)

    Session("myPlaceholders") = lstPlaceHolders
End Sub

Protected Sub btnClearList_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnClearList.Click
    Session.Remove("myPlaceholders")
    pnlAddPLO.Controls.Clear()
End Sub

Don't forget to clear your session variable if you don't want the placeholders to show every time you come back to this page during the 20 minutes (default value) your session variable lasts. In my code, I added a clear button, but maybe that's not what you want.

Community
  • 1
  • 1
actaram
  • 2,038
  • 4
  • 28
  • 49
  • This is the most progress I've had in hours, and was beginning to think about just having 1 textbox that would store each entry string in a vector (or list), then use a ListView to pull those items from the array for editing/viewing beneath this "entry" textbox. I will be citing this answer as a source in my comments, btw. I just have to get my label text and placeholderID enumeration to work properly with this. Once I get to the third iteration of a textbox, I get an error because the placeholder IDs are not unique. Although, I'm not sure if that ListView idea I had would be simpler... – Krizz Apr 14 '15 at 20:23
  • Would it be simpler? I don't think so, but that's subjective. As of your error, it's not normal; I don't have this problem. Can you show me your code (with [pastebin](http://pastebin.com/))? – actaram Apr 14 '15 at 20:37
  • It was a small fix. I forgot to change one of the old counters, and I needed to remove a placeholder that I had on the page for testing. Thanks again! – Krizz Apr 15 '15 at 13:02