I am working on a project that I want to "+Add Row" to. This will Add a row of four fields that are textboxes. The # of rows is variable so I created a literal control and have been doing

Protected Sub btnAddRow_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddRow.Click
        Dim OfficeEquipmentRow As String = "<div class='EquipmentListing Equipment'><input type='text' Class='WorksheetInputStyle' ID='OfficeEquipment" & Session("OfficeRow") & "' runat='server'></div>"
        Dim OfficePriceRow As String = "<div class='EquipmentListing Price'><input type='text' Class='WorksheetInputStyle' ID='OfficePrice" & Session("OfficeRow") & "' runat='server'></div>"
        Dim OfficeQuantityRow As String = "<div class='EquipmentListing Quantity'><input type='text' Class='WorksheetInputStyle' ID='OfficeQuantity" & Session("OfficeRow") & "' runat='server'></div>"
        Dim OfficeTotalRow As String = "<div class='EquipmentListing Totals'><span Class='Totals' ID='OfficeTotal" & Session("OfficeRow") & "' runat='server'>$</span></div>          "

        litUpdate.Text += OfficeEquipmentRow & OfficePriceRow & OfficeQuantityRow & OfficeTotalRow
        Session("OfficeRow") = Session("OfficeRows") + 1
    End Sub

The only issue is when I hit Add Button again all the data that may have been input into the literal control's text boxes disappears. This will be very very annoying to the end user. How can I retain this information?

I was able to resolve this issue on my own and with a bit of a point in the right direction. Here is the code to what I ended up doing. Hope it can help someone sometime! :D

Protected Sub btnAddRow_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddRow.Click
        Session("row_click") = Session("row_click") + 1
        lblDate.Text = Session("row_click")
    End Sub


    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        add_rows()
    End Sub

    Public Function add_rows() As String

        Dim cnt As Integer = Session("row_click")
        lblPurchasePrice.Text = cnt.ToString
        'If Session("row_click") > 1 Then
        For x = 1 To cnt
            Dim OfficeEquipmentRow, OfficePriceRow, OfficeQuantityRow As TextBox
            Dim OfficeTotalRow As Label = New Label
            OfficeEquipmentRow = New TextBox
            OfficePriceRow = New TextBox
            OfficeQuantityRow = New TextBox

            phOfficeEquipment.Controls.Add(OfficeEquipmentRow)
            phOfficeEquipment.Controls.Add(OfficePriceRow)
            phOfficeEquipment.Controls.Add(OfficeQuantityRow)
            phOfficeEquipment.Controls.Add(OfficeTotalRow)

            OfficeEquipmentRow.ID = "OfficeEquipment" & x
            OfficePriceRow.ID = "OfficePrice" & x
            OfficeQuantityRow.ID = "OfficeQuantity" & x
            OfficeTotalRow.ID = "OfficeTotal" & x

            OfficeEquipmentRow.CssClass = "EquipmentListing Equipment"
            OfficePriceRow.CssClass = "EquipmentListing Price"
            OfficeQuantityRow.CssClass = "EquipmentListing Quantity"
            OfficeTotalRow.CssClass = "Totals"
        Next
        'set_text()
        'End If
        Return vbNull
    End Function
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Session("row_click") = vbNull Then
            Session("row_click") = 1
        End If
        If Session("row_click") = 0 Then
            Session("row_click") = 1
        End If

    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.