1

每次我更改网页上的下拉菜单时,页面都会“重置”。我以编程方式插入的一行消失了,并写入了一个新行。我想让用户添加多行。

我正在尝试

  1. 使用下拉列表填充显示产品信息的多个 GridView
  2. 用户将在文本框中添加数量
  3. 点击“添加到购物车”,这将...
  4. 使用所选产品详细信息在该页面上的现有表上创建一个新行

这仅适用于第一行,但是,如果我更改下拉列表或单击“添加到购物车”按钮,新插入的行将被后续更新的行覆盖。

这是我的桌子...

<asp:Table ID="tblOrderPreview" runat="server" BorderStyle="Solid" Width="800px" >
    <asp:TableHeaderRow BorderStyle= "Solid"><asp:TableHeaderCell>Product</asp:TableHeaderCell><asp:TableHeaderCell>Qty</asp:TableHeaderCell><asp:TableHeaderCell>Price</asp:TableHeaderCell><asp:TableHeaderCell>Total</asp:TableHeaderCell><asp:TableCell><b>Remove Item</b></asp:TableCell></asp:TableHeaderRow>
    <asp:TableRow BorderStyle= "Solid"><asp:TableCell></asp:TableCell><asp:TableCell></asp:TableCell><asp:TableCell></asp:TableCell><asp:TableCell></asp:TableCell><asp:TableCell></asp:TableCell></asp:TableRow>
</asp:Table>

这是按钮调用的代码:

Protected Sub btnAddToCart_Click(sender As Object, e As System.EventArgs) Handles btnAddToCart.Click
    lblValidator.Visible = False


        If txtQuantity.Text <> "" Then

            lblValidator.Visible = False

        Dim name As String
        name = GridView3.Rows(0).Cells(0).Text.ToString

        Dim qantity As Integer
        qantity = Convert.ToDouble(txtQuantity.Text)

        Dim price As String
        price = Convert.ToDouble(GridView5.Rows(0).Cells(0).Text.ToString)

        Dim total As String
        total = "$" + (price * qantity).ToString

        'insert new row to tblOrderPreview (count rows, then insert another row named COUNT+1
        Dim tRow As New TableRow()
        tblOrderPreview.Rows.Add(tRow)

        Dim tCellProduct As New TableCell()
        tCellProduct.Text = name
        tRow.Cells.Add(tCellProduct)

        Dim tCellQty As New TableCell()
        tCellQty.Text = qantity.ToString
        tRow.Cells.Add(tCellQty)

        Dim tCellPrice As New TableCell()
        tCellPrice.Text = price
        tRow.Cells.Add(tCellPrice)

        Dim tCellTotal As New TableCell()
        tCellTotal.Text = total
        tRow.Cells.Add(tCellTotal)

        Dim tCellRemove As New TableCell()
        tCellRemove.Text = "del!"
        tRow.Cells.Add(tCellRemove)


    Else
        lblValidator.Visible = True

    End If

End Sub
4

1 回答 1

1

只是为了澄清我上面的评论并显示一些代码:

Dim tblRowColl As TableRowCollection = tblOrderPreview.Rows

'Run the code that gets the new row

For Each tblRow As TableRow In tblRowColl
    tblOrderPreview.Rows.Add(tblRow)
Next

希望这会有所帮助。

谢谢,

第一岬

于 2013-06-12T07:26:07.823 回答