|
Hot-Link Menu in Right-Side Column |
Wal Mart Online → Store Pickup → Free Shipping![]() |
|||||||||||||||||||||||||||||||
|
Add CartItem ClassThe next step is to add the CartItem class. In case you've forgotten how to add a class, you can review how we created the Product Class. In a similar, or even the exact same, manner, create the CartItem Class with the code below:
Imports Microsoft.VisualBasic
Public Class CartItem
Public SelectedItem As Product
Public Quantity As Integer
Public Function Display() As String
Return SelectedItem.Name & " (" &Quantity.ToString() & " at " _
& FormatCurrency(SelectedItem.UnitPrice) & " each)"
End Function
End Class
After the above code is entered, and you have set Order.aspx.vb as the start page, run the program, to make sure everything is still good. Although, we have entered the class, since we aren't using it, it should have no effect yet. Change the selected product to Hippie and the screen you should be seeing is:
Wow, what a cutie!!! So that's what happens when you have 40 years of Free Love, Peace and Doing Your Own Thing. Where do I sign up?
Next, we will set the PostBackUrl property of the Continue Shopping button on the DisplayCart page, so that it jumps back to the order page. Performing a Single-Click on the ContinueShopping button will bring up the Properties Page somewhere on your screen. Left-Click on the values column of the PostBackUrl property on the btnContinue properties page as indicated in the diagram below:
Press the Select Url button to the right of the PostBackUrl Value Column (right where the arrow is pointing), to bring up the Select URL dialog box as indicated in the diagram below:
Switch back to the Design View page for Orders.aspx and change the PostBackUrl on the Go To Cart button to jump to the DisplayCart page, like we just did for the Continue Shopping button in the example above. Completing a circuit where we can freely navigate between the pages. To make your personal navigation experience safer and more rewarding, check out the latest in marine navigation equipment available through Centurion Online → Electronics → Marine ElectronicsOnce you have divested yourself of all that unnecessary disposable capital @ Centurion Online, come back and run the program jumping between the pages using the Go To Cart and Continue Shopping buttons. Create GetCartContents Function Using Session StateA function to return the Cart Contents is needed. For this application, we will use SessionState to hold the contents of the cart. Enter the following function in the Order.aspx.vb module:
'Function to return the contents of the shopping cart held in session state
Private Function GetCartContents() As SortedList
'Create a variable to hold the session name so it will be insertedable by Intellisense.
Dim SessionName As String = "Cart"
'If no shopping cart exists yet, create one.
If Session(SessionName) Is Nothing Then
Session.Add(SessionName, New SortedList)
End If
'Convert Session Variable to SortedList Data Type and return
Return CType (Session(SessionName), SortedList)
End Function
It is good practice to run the code as you add the functions, just to make sure you aren't making any fatal errors along the way. Run the program, and change a few selections, to make sure you haven't lost functionality as we proceed. I try to add new functions, so that as I add them, the code still functions, which means entering code at the lowest level first, then working your way up.
The next code snippet we need to add is a Sub to add an individual item to the Users Shopping Cart. Enter the code below to process adding an item to the cart:
'Sub to add selected item to the Current Shopping Cart Contents
Private Sub AddToCart(ByVal ItemToAdd As CartItem)
'Create a SortedList container for the selected item.
Dim CurrentCartContents As SortedList
'Create a String to Hold the Product ID
Dim ProductCode As String
'Call Function to Place Selected Item in SortedList Data Type
CurrentCartContents = Me.GetCartContents
'Get The ProductID to use as a key
ProductCode = SelectedProduct.ProductID
'Check to see if item is already in Cart
If CurrentCartContents.ContainsKey(ProductCode) Then
ItemToAdd = CType(CurrentCartContents(ProductCode), CartItem)
ItemToAdd.Quantity += CInto(txtQuantity.Text)
Else
CurrentCartContents.Add(ProductCode, ItemToAdd)
End If
End Sub
If you would like like to see a shopping cart in action, so you can see where we are headed with this, take a visit to:
In order to continue with this guide, and continue the discussion on Adding the Shopping Cart Class Press the Button below:
|
Home Visual Basic Introduction to VB.NET.NET Framework VS2008 IDE How VB is Compiled Start Visual Studio Windows Form App Save Your Work VB OOP ProgrammingVisual Basic Code Exit Code Button Event Code Coding RecommendationsIf/Then/Else Error List Window Comment Syntax Help Window Language Essentianl Built-In Data Types Declare Variables Declare ConstantsCode Arithmetic Expressions Assignment Statements Operator Precedence Type Casting Math Class String DeclarationConversion Functions Conversion Methods Formatting Functions String Formatting Variable Scope EnumerationsNullable Types Loop Constructs For Next LoopDo While Loop Do Until Loop Do...Loop-WhileDo...Loop-Until Exit Do | Exit For Do...LoopNested Loops Arrays Array DeclarationRnd( ) Function Listbox ControlKeyPressEventArgs Parallel Arrays Key Event ArgsDynamic Arrays Redimension ArraySet Breakpoint Start Debugger ReDim Preserve MultiDimensional Arrays DataGridView ControlLength and Sort Methods Structures Pad RightSplit Method IsNumeric Function Multiform Projects Add Form To Project Form Object Methods Form Show Method ShowDialog Method Form Close Method Form Accept Button Multiform Project Example ASP.NET Web Programming Create Data SourceConfigure Access Data Source Add Product Class Extract Local Database Data Order PageLoad VB CodeAdd New Web Page Set Start Page Display Cart Aspx CodeDisplay Cart Design View Sorted List Definition VB.NET Session State Create CartItem Class GetCartContents FunctionAdd To Cart Event Handler Remove Cart Item EventClear Cart Event Handler |