开发者

ASP.NET Populate Gridview Textfields with values from session shopping cart

Hey so I have a gridview as follows

<asp:GridView ID="productListTable开发者_JAVA技巧" 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>

And then I also have a Session Shopping Cart which has CartItems, and this itself works fine. Now what I am trying to do is that when users add products to their shopping cart and toggle between pages that I prepopulate the quantity field textbox in the gridview with the values of the cartitems quantity where the 2 products match i.e. this is what I was trying

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

And it actually seems to work on first instance (altho im not sure the above is a good way to do it, but the problem is when use the pagination on the gridview I get the error

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

On the line

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

Any idea why I am getting this error and also is there a better way to do it ?


Probably because not all rows have a datakey, only the rows that contain data (so not your headerrow, footer, etc.) Try to add this condition:

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

It would be better however to place this kind of code in the RowDataBound eventhandler, that way you are always sure that you handle all rows directly after they are added to the grid:

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜