1

I created an ASP Table Control and added a row to it programatically. I then repeated the process to add another row. The first row that I added disappeared and only the newest row is being displayed. Is there a property that needs to be set to allow the state of the table to be saved?

EDIT:

All I do is click a button on the page and it adds a row to the table. Repeated clicks of the button only add one from to the table. If it helps, here is the function that adds the rows.

 Protected Sub addItemButton(sender As Object, e As EventArgs)
    'get the id of the item they selected
    Dim id As String = CType(sender, Button).Text
    'clear out the data being displayed
    gridViewItemSearchItems.DataSource = Nothing
    gridViewItemSearchItems.DataBind()
    'add this item to the line table
    Dim itemToAdd As Item = gp.Items.GetItemByKey(id)

    Dim tableRow As New System.Web.UI.WebControls.TableRow()

    Dim cellId As New System.Web.UI.WebControls.TableCell
    cellId.Text = itemToAdd.Key.Id

    Dim cellDesc As New System.Web.UI.WebControls.TableCell
    cellDesc.Text = itemToAdd.Description

    Dim cellMSRP As New System.Web.UI.WebControls.TableCell
    cellMSRP.Text = gp.Items.GetItemMSRP(itemToAdd.Key)

    Dim cellCost As New System.Web.UI.WebControls.TableCell
    cellCost.Text = itemToAdd.CurrentCost.Value

    Dim cellUnitPrice As New System.Web.UI.WebControls.TableCell
    cellUnitPrice.Text = itemToAdd.StandardCost.Value

    Dim cellDiscount As New System.Web.UI.WebControls.TableCell
    cellDiscount.Text = "12%"

    Dim cellQuantity As New System.Web.UI.WebControls.TableCell
    cellQuantity.Text = "1"

    tableRow.Cells.Add(cellId)
    tableRow.Cells.Add(cellDesc)
    tableRow.Cells.Add(cellMSRP)
    tableRow.Cells.Add(cellCost)
    tableRow.Cells.Add(cellUnitPrice)
    tableRow.Cells.Add(cellDiscount)
    tableRow.Cells.Add(cellQuantity)

    tblItems.Rows.Add(tableRow)
End Sub
CAbbott
  • 8,078
  • 4
  • 31
  • 38
user489041
  • 27,916
  • 55
  • 135
  • 204

1 Answers1

3

Since you're adding the rows to your table dynamically, they have to be re-created on each successive post-back.

Look at this answer for an explanation as to why dynamically added controls can disappear: ASP.NET dynamically created controls and Postback

EDIT: It is possible to dynamically add controls during a post-back, my answer here shows a way to do that using ViewState. Although, this does become very cumbersome. You might want to re-think the design of the page.

Community
  • 1
  • 1
CAbbott
  • 8,078
  • 4
  • 31
  • 38