ASP.NET Filling in textfields in Gridview from session variables for ShoppingCart
<p>In my gridview I list of bunch of products from database which is loaded by a Stored Procedure.
So in the gridview I list i.e. Product Name, Price, Description etc but then I have a textfield called quantity where the user can enter in a quantity and when they press update I then insert this as a cartitem in my shoppingcart</p>

<pre><code>Protected Sub btnUpdateCart_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpdateCart.Click
    For Each row As GridViewRow In gvShoppingCart.Rows
        If row.RowType = DataControlRowType.DataRow Then
            ' We'll use a try catch block in case something other than a number is typed in. If so, we'll just ignore it.
            Try
                ' Get the productId from the GridView's datakeys
                Dim productId = Convert.ToInt32(gvShoppingCart.DataKeys(row.RowIndex).Value)
                ' Find the quantity TextBox and retrieve the value
                Dim quantity = Integer.Parse(CType(row.Cells(1).FindControl("txtQuantity"), TextBox).Text)
                ShoppingCart.Instance.SetItemQuantity(productId, quantity)
            Catch ex As FormatException
            End Try
        End If
    Next

    BindData()
End Sub
</code></pre>

<p>As such but then when I flick between the pages and i.e. shoppingcart.aspx and checkout.aspx I loose the values entered in those checkboxes, so what I am looking to do is everytime I load the page with the gridview is to check for my session shoppingcart and see if product id matches between row and session and if they do then enter that quantity dynamically back in textbox. heres my gridview</p>

<pre><code>&lt;asp:GridView runat="server" ID="gvShoppingCart" AutoGenerateColumns="false" EmptyDataText="There is nothing in your shopping cart." AlternatingRowStyle-CssClass="tr_dark"  HeaderStyle-CssClass="header_req" BorderWidth="0px" GridLines="None" AllowPaging="true" PageSize="25" AllowSorting="false" Width="100%" ShowFooter="true" DataKeyNames="ProductId" OnRowDataBound="gvShoppingCart_RowDataBound" OnRowCommand="gvShoppingCart_RowCommand"&gt;

    &lt;Columns&gt;                         
            &lt;asp:TemplateField HeaderText="Product Name" HeaderStyle-Width="130px" SortExpression="productName"&gt;
                &lt;ItemTemplate&gt;   
                &lt;asp:Label ID="ProductNameField" runat="server" Text='&lt;%# Eval("description").ToString() %&gt;'&gt;&lt;/asp:Label&gt;
                &lt;/ItemTemplate&gt;                     
            &lt;/asp:TemplateField&gt; 

    &lt;/Columns&gt;
    &lt;Columns&gt;                         
            &lt;asp:TemplateField HeaderText="Pack Size" HeaderStyle-Width="70px" SortExpression="packSize"&gt;
                &lt;ItemTemplate&gt;  
                &lt;asp:Label ID="PackSizeField" runat="server" Text='&lt;%#  Eval("packSize").ToString()%&gt;'&gt;&lt;/asp:Label&gt;
                &lt;/ItemTemplate&gt;                     
            &lt;/asp:TemplateField&gt; 

    &lt;/Columns&gt;
    &lt;Columns&gt;                         
            &lt;asp:TemplateField HeaderText="Stock" HeaderStyle-Width="130px" SortExpression="address"&gt;
                &lt;ItemTemplate&gt;   
                &lt;asp:Label ID="TradePriceField" runat="server" Text='&lt;%#   DisplayStockLevel(Eval("StockIndicator").ToString()) %&gt;'&gt;&lt;/asp:Label&gt;
                &lt;/ItemTemplate&gt;                     
            &lt;/asp:TemplateField&gt; 

    &lt;/Columns&gt;
    &lt;Columns&gt;
    &lt;asp:TemplateField HeaderText="Quantity" HeaderStyle-Width="10px"&gt;
        &lt;ItemTemplate&gt;
            &lt;asp:TextBox runat="server"  Width="30" ID="txtQuantity" Text='&lt;%# Eval("Quantity") %&gt;'&gt;&lt;/asp:TextBox&gt;
        &lt;/ItemTemplate&gt;
    &lt;/asp:TemplateField&gt;
    &lt;/Columns&gt;

    &lt;Columns&gt;
    &lt;asp:BoundField DataField="UnitPrice" HeaderText="Actual Price" ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Right" DataFormatString="{0:C}" /&gt;
    &lt;asp:BoundField DataField="TotalPrice" HeaderText="Total" ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Right" DataFormatString="{0:C}" /&gt;
    &lt;/Columns&gt;
    &lt;Columns&gt;
    &lt;asp:TemplateField HeaderText=""&gt;
    &lt;ItemTemplate&gt;
        &lt;asp:ImageButton  ImageUrl="~/img/icons/cross.gif"  width="10" height="10" alt="Cancel" runat="server" ID="btnRemove" CommandName="Remove" CommandArgument='&lt;%# Eval("ProductId") %&gt;'/&gt;
    &lt;/ItemTemplate&gt;
    &lt;/asp:TemplateField&gt;
    &lt;/Columns&gt;
</code></pre>

<p></p>
