ASP表不保存状态

时间:2012-01-30 22:10:13

标签: asp.net vb.net

我创建了一个ASP Table Control并以编程方式向其添加了一行。然后我重复该过程以添加另一行。我添加的第一行消失了,只显示了最新的一行。是否需要设置一个属性以允许保存表的状态?

编辑:

我所做的就是单击页面上的一个按钮,它会在表格中添加一行。重复单击该按钮只会向表中添加一个。如果有帮助,这里是添加行的函数。

 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

1 个答案:

答案 0 :(得分:3)

由于您要动态地将行添加到表中,因此必须在每个连续的回发时重新创建它们。

请查看此答案,了解为何动态添加的控件可能会消失:ASP.NET dynamically created controls and Postback

编辑:可以在回发期间动态添加控件,我的回答here显示了使用ViewState执行此操作的方法。虽然,这确实变得非常麻烦。您可能想要重新考虑页面的设计。