Hi John,
Glad you like the book. Would you be willing to share your C# version of the code with other Wrox forum visitors?
The problem with the solution you are proposing is that it's no longer single row based; e.g. a user may now be able to update multiple rows at once (depending on how you implement it). This means it's no longer easy to let the GridView handle the Cart update automatically (although still possible). To force the GridView and the Cart to be updated, you could add an Update Cart button that loops through the items in the cart and updates them. Here's a break down of how you could implement it:
1. Move the DropDownList for the quantity into the ItemTemplate.
2. Remove the AutoPostBack and OnSelectedIndexChanged attributes from this drop down so it becomes a simple DropDownList.
3. Below the GridView add a new button called btnUpdate. Set its Text to something like Update Cart.
4. Double click the button in Design View and add the following code:
Code:
Protected Sub btnUpdate_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button1.Click
For Each myRow As GridViewRow In GridView1.Rows
' Find the Id of the current product
Dim key As Guid = New Guid(GridView1.DataKeys(myRow.DataItemIndex).Value.ToString())
' Find the DropDownList with the quanity (old or new)
Dim myControl As DropDownList = myRow.FindControl("lstQuantity")
' Get the chosen quantity
Dim newQuantity As Integer = Convert.ToInt32(myControl.SelectedValue)
' Update each product by calling the Update method, just as the ODS used to do
ShopManager.ShoppingCart.Update(newQuantity, key)
Next
' Tell the world the cart has changed
RaiseEvent CartUpdated(sender, New System.EventArgs())
End Sub
It's
VB.NET code only, but if you were able to convert the entire Shop, I don't think this poses a problem. If it does, let me know and I'll rewrite it in C#.
Cheers,
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of
Beginning ASP.NET 3.5 : in C# and VB,
ASP.NET 2.0 Instant Results and
Dreamweaver MX 2004
Want to be my colleague? Then check out this post.