Imports System.Data
Partial Class Orders
Inherits System.Web.UI.Page
Private SelectedProduct As Product
Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
ddlProducts.DataBind()
End If
SelectedProduct = Me.GetSelectedProduct()
lblName.Text = SelectedProduct.Name
lblShortDescription.Text = SelectedProduct.ShortDescription
lblLongDescription.Text = SelectedProduct.LongDescription
lblUnitPrice.Text = FormatCurrency(SelectedProduct.UnitPrice)
imgProduct.ImageUrl = "Images\Products\" _
& SelectedProduct.ImageFile
End Sub
Private Function GetSelectedProduct() As Product
Dim dvTable As DataView = CType(AccessDataSource1.Select( _
DataSourceSelectArguments.Empty), DataView)
dvTable.RowFilter = "ProductID = '" & ddlProducts.SelectedValue & "'"
Dim drvRow As DataRowView = dvTable(0)
Dim Product As New Product
Product.ProductID = drvRow("ProductID").ToString
Product.Name = drvRow("Name").ToString
Product.ShortDescription = drvRow("ShortDescription").ToString
Product.LongDescription = drvRow("LongDescription").ToString
Product.UnitPrice = CDec(drvRow("UnitPrice"))
Product.ImageFile = drvRow("ImageFile").ToString
Return Product
End Function
Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
If Page.IsValid Then
Dim CartItem As New CartItem
CartItem.Product = SelectedProduct
CartItem.Quantity = CType(txtQuantity.Text, Integer)
Me.AddToCart(CartItem)
Response.Redirect("Cart.aspx")
End If
End Sub
Private Sub AddToCart(ByVal CartItem As CartItem)
Dim Cart As SortedList = GetCart()
Dim sProductID As String = SelectedProduct.ProductID
If Cart.ContainsKey(sProductID) Then
CartItem = CType(Cart(sProductID), CartItem)
CartItem.Quantity += CType(txtQuantity.Text, Integer)
Else
Cart.Add(sProductID, CartItem)
End If
End Sub
Private Function GetCart() As SortedList
If Session("Cart") Is Nothing Then
Session.Add("Cart", New SortedList)
End If
Return CType(Session("Cart"), SortedList)
End Function
End Class
Partial Class Cart
Inherits System.Web.UI.Page
Private Cart As SortedList
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Cart = GetCart()
If Not IsPostBack Then
Me.DisplayCart()
End If
End Sub
Private Function GetCart() As SortedList
If Session("Cart") Is Nothing Then
Session.Add("Cart", New SortedList)
End If
Return CType(Session("Cart"), SortedList)
End Function
Private Sub DisplayCart()
firstCart.Items.Clear()
Dim CartItem As CartItem
Dim CartEntry As DictionaryEntry
For Each CartEntry In Cart
CartItem = CType(CartEntry.Value, CartItem)
firstCart.Items.Add(CartItem.Display)
Next
End Sub
Protected Sub btnRemove_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRemove.Click
If firstCart.SelectedIndex > -1 And Cart.Count > 0 Then
Cart.RemoveAt(firstCart.SelectedIndex)
Me.DisplayCart()
End If
End Sub
Protected Sub btnEmpty_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEmpty.Click
Cart.Clear()
firstCart.Items.Clear()
lblMessage.Text = ""
End Sub
Protected Sub btnCheckOut_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCheckOut.Click
lblMessage.Text = "Sorry, that function hasn't been implemented yet."
End Sub
End Class
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment