【问题标题】:ASP Table doesnt save stateASP 表不保存状态
【发布时间】:2012-01-30 22:10:12
【问题描述】:

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

编辑:

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

 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 在单击按钮后完成。所以在 OnClick 函数之后

标签: asp.net vb.net


【解决方案1】:

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

查看这个答案,了解为什么动态添加的控件会消失:ASP.NET dynamically created controls and Postback

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

【讨论】:

  • 太棒了,感谢您的帮助。我现在要试一试。
猜你喜欢
  • 2018-11-10
  • 1970-01-01
  • 1970-01-01
  • 2023-02-15
  • 1970-01-01
  • 1970-01-01
  • 2014-02-23
  • 1970-01-01
  • 2012-09-13
相关资源
最近更新 更多