ASP.NET使用会话购物车中的值填充Gridview文本字段

时间:2011-08-05 09:28:53

标签: asp.net vb.net

嘿所以我有一个gridview如下

<asp:GridView ID="productListTable" runat="server" DataSourceID="srcProductListPerCustomer" AutoGenerateColumns="False" AlternatingRowStyle-CssClass="tr_dark"  HeaderStyle-CssClass="header_req" BorderWidth="0px" GridLines="None" AllowPaging="true" PageSize="25" EmptyDataText="No records." AllowSorting="false" Width="100%" DataKeyNames="product_ID_key">         
    <Columns>                         
            <asp:TemplateField HeaderText="Product Name" HeaderStyle-Width="250px" SortExpression="productName" ItemStyle-CssClass="product_name" >
                <ItemTemplate>   
                <asp:Label ID="ProductNameField" runat="server" Text='<%# Eval("productName").ToString() %>'></asp:Label>
                </ItemTemplate>                     
            </asp:TemplateField> 

    </Columns>
     <Columns>  

    <asp:TemplateField HeaderText="Quantity">
                    <ItemTemplate>
                        <asp:TextBox runat="server" ID="txtQuantity" Columns="5"></asp:TextBox><br />
                        <asp:LinkButton runat="server" ID="btnRemove" Text="Remove" CommandName="Remove" CommandArgument='<%# Eval("product_ID_key") %>' style="font-size:12px;"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
    </Columns>
    <HeaderStyle CssClass="header_req" />
    <AlternatingRowStyle CssClass="tr_dark" />
    <PagerStyle CssClass="pagination" />
    <PagerSettings PageButtonCount="3" FirstPageText="First" LastPageText="Last" NextPageText="Next" PreviousPageText="Previous" Mode="NumericFirstLast" />      
</asp:GridView>

然后我还有一个具有CartItems的会话购物车,这本身也可以。现在我要做的是,当用户将产品添加到他们的购物车并在我在网格视图中预填充数量字段文本框的页面之间切换时,2个产品匹配的cartitems数量的值,这就是我正在尝试的

Protected Sub Page_PreRender(ByVal s As Object, ByVal e As EventArgs)Handles Me.PreRender
    'code here
    For i As Integer = 0 To Me.productListTable.Rows.Count - 1
        Dim txtQuantity As TextBox = CType(productListTable.Rows(i).FindControl("txtQuantity"), TextBox)

        Dim productId As String = productListTable.DataKeys(i).Value
        ' Find the item and update the quantity
        Dim updatedItem = New CartItem(productId)

        For Each item As CartItem In ShoppingCart.Instance.Items
            If item.Equals(updatedItem) Then
                txtQuantity.Text = item.Quantity
            End If
        Next
    Next
End Sub

它实际上似乎在第一个实例上工作(虽然我不确定上面是一个很好的方法,但问题是当在gridview上使用分页时我得到错误

指数超出范围。必须是非负数且小于集合的大小。 参数名称:index

在线

Dim productId As String = productListTable.DataKeys(i).Value

我知道为什么会收到此错误,还有更好的方法吗?

1 个答案:

答案 0 :(得分:0)

可能是因为并非所有行都有数据键,只有包含数据的行(所以不是你的页眉,页脚等) 尝试添加此条件:

If productListTable.Rows(i).RowType = DataControlRowType.DataRow Then
    Dim txtQuantity As TextBox = CType(productListTable.Rows(i).FindControl("txtQuantity"), TextBox)

    Dim productId As String = productListTable.DataKeys(i).Value
    ' Find the item and update the quantity
    Dim updatedItem = New CartItem(productId)

    For Each item As CartItem In ShoppingCart.Instance.Items
        If item.Equals(updatedItem) Then
            txtQuantity.Text = item.Quantity
        End If
    Next

End If

然而,将这种代码放在RowDataBound事件处理程序中会更好,这样您总是可以确保在将所有行添加到网格后直接处理它们:

Protected Sub productListTable_RowDataBound(ByVal s As Object, ByVal e As GridViewRowEventArgs)
   If e.Row.RowType = DataControlRowType.DataRow Then
       Dim txtQuantity As TextBox = CType(e.Row.FindControl("txtQuantity"), TextBox)

       Dim productId As String = productListTable.DataKeys(e.Row.RowIndex).Value
       ' Find the item and update the quantity
       Dim updatedItem = New CartItem(productId)

       For Each item As CartItem In ShoppingCart.Instance.Items
           If item.Equals(updatedItem) Then
               txtQuantity.Text = item.Quantity
           End If
       Next

   End If
End Sub