2012-01-30 93 views
1

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

编辑:

我所要做的就是点击页面上的一个按钮,它增加了一个排表。重复点击该按钮只能从表格中添加一个。如果有帮助,这是添加行的函数。

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 
+0

你如何添加行(在什么情况下)?你能发布你的代码吗? – CAbbott 2012-01-30 22:13:07

+0

@CAbbott在点击按钮后完成。所以在一个OnClick函数后 – user489041 2012-01-30 22:13:50

回答

3

由于您要动态地将行添加到表中,所以必须在每个后续回发中重新创建它们。

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

编辑:这是可能的一个后回时动态添加控件,我的回答here显示的方式做即使用ViewState。尽管如此,这确实变得非常麻烦。您可能想重新考虑页面的设计。

+0

太棒了,感谢您的帮助。我现在要给这个镜头一个镜头。 – user489041 2012-01-31 18:15:04