Hot-Link Menu in Right-Side Column

Wal Mart Online → Store Pickup → Free Shipping
WM468X60_ko_static.gif


Event Handler Code for AddToCart Button

All the groundwork should have been laid so we can now enter the code for the AddToCart Event Handler. Double-Click the AddToCart button and enter the following code:

'Event Handler for AddToCart Button on Order.aspx Web Page
Protected Sub btnAddToCart_Click(ByVal sender As Object, _
        ByVal e As EventArgs) Handles btnAddToCart.Click

    'Declare CartItem Class Variable (ItemToAdd)
    Dim ItemToAdd As New CartItem
    
    'Get Data About Selected Item
    ItemToAdd.SelectedItem = SelectedProduct

    'Determine Quantity Ordered
    ItemToAdd.Quantity = CInt(txtQuantity.Text)

    'Add Entry to Shopping Cart
    Me.AddToCart(ItemToAdd)

    'Display Web Page: DisplayCart.aspx
    Response.Redirect("DisplayCart.aspx")

End Sub

Well, we are getting closer, but, as you can see if we run the program, the ListBox on the DisplayCart page, isn't updating just yet. Enter the following sub which will display the shopping cart contents in the ListBox on the DisplayCart.aspx Web Page:


'Declare a Class Level Variable of the CartContents for all of the functions to access.
Private CartContents As SortedList

'Sub to Display Cart Contents to ListBox
Private Sub DisplayCart()

    'Clear the ListBox
    lstCart.Items.Clear()

    'Declare a CartItem Class Variable
    Dim CartEntry As CartItem

    'Display CartContents in ListBox
    For Each Item As DictionaryEntry In CartContents
        CartEntry = CType(Item.Value, CartItem)
        lstCart.Items.Add(CartEntry.Display)
    Next
End Sub

Buy from Centurion


The next function we will need is to pull the Current Shopping Cart contents out of Session State.

Create GetCartContents Function Using Session State

A 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 DisplayCart.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

And finally, to get the contents of the cart to display in the listbox on the DisplayCart web page enter the following code as the load page event handler:


'Load Page Event Handler, requests contents of shopping cart to be displayed
Private Sub DisplayCart_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    'Pull the Cart Contents out of Session State
    CartContents = Me.GetCartContents

    'Display CartContents in the ListBox
    If Not IsPostBack Then
        Me.DisplayCart()
    End If

End Sub

Run the application, and add items to the cart, checking that the quantity increments correctly, as indicated in the diagram below:

Help Support Mr. Centurion - Donate or Visit any Sponsor

No Purchase Necessary.

Help us keep this site operational by making a donation. Any amount is greatly appreciated. OR, if you visit our sponsor, they will make a donation for you, just for visiting.

Thank you



Shopping Cart Displaying Sample Items in VB.NET Web Program

Figure CC-24: Shopping Cart Displaying Sample Items on DisplayCart Web Page


If all goes well, the listing of the cart contents should be updating correctly in sync with the Add To Cart button. If not, cut and paste the code in the previous examples, as that code was used to generate the sample displayed on the screen.

Excellent Tech Support - Centurion Approved

The preceding shopping cart was a retailers dream come true: You can keep adding items to the cart, but you can't take them away. Unfortunately, this may irritate customers to the point where they don't want to buy anything from you at all. So, I guess we are forced to include a Remove Item option, that is indicated by the Remove Button on the DisplayCart Web Page. Double-click the Remove Button to create the template for the remove button, and fill in the following code, which will remove the highlighted item from the Listbox:

Shopping Cart Remove Item Event Handler


'Event Handler for Remove Button to Remove Selected Item from ListBox
Protected Sub btnRemove_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnRemove.Click

    'Check to see if Cart is Empty
    If CartContents.Count > 0 Then

        'Check to see if an item is selected to be deleted
        If lstCart.SelectedIndex > -1 Then

            'Remove the Selected Item
            CartContents.RemoveAt(lstCart.SelectedIndex)
        Me.DisplayCart()
            lblMessage.Text=""
        Else
            'Tell the user to select something if they want it removed.
            lblMessage.Text="Please select the item you want to remove."
        End If
    Else
        lblMessage.Text=""
    End If
End Sub

Run the above code and place your favorite items in your cart. A typical cart with 3 items may look like the diagram below:

Example Shopping Cart Before Remove Item Operation

Figure CC-25: Example Shopping Cart Before Remove Item Operation


Visit Our Sponsor to Help Support Our Site - No Purchase Necessary


Looking over the contents in my cart, and then reviewing my available capital for such a worthwhile expenditure, I notice that I am running a little short in my Halloween paraphenalia account. So, I guess I am going to have to do without those 4 severed hands, making this upcoming Halloween, a very bleak event, indeed. Unless, of course, some kind soul wishes to help out by clicking on some Google Ads, so I can puff that account back up. In the meantime, I have selected to place the Severed Heads item on the chopping block, prepared for the dreaded Remove Item event. Doing so gives the diagram illustrated below:

Example Shopping Cart After Remove Item Operation

Figure CC-26: Example Shopping Cart After Remove Item Operation


In order to continue with this guide, and begin the discussion on Emptying The Shopping Cart Press the Button below:


Visit Our Sponsor to Help Support Our Site - No Purchase Necessary


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 Programming

Visual Basic Code


Exit Code


Button Event Code

Coding Recommendations

If/Then/Else

Error List Window

Comment Syntax

Help Window

Language Essentianl

Built-In Data Types


Declare Variables

Declare Constants

Code Arithmetic Expressions

Assignment Statements

Operator Precedence

Type Casting


Math Class

String Declaration

Conversion Functions

Conversion Methods

Formatting Functions

String Formatting


Variable Scope

Enumerations

Nullable Types


Loop Constructs

For Next Loop

Do While Loop


Do Until Loop

Do...Loop-While

Do...Loop-Until


Exit Do | Exit For

Do...Loop


Nested Loops


Arrays

Array Declaration


Rnd( ) Function

Listbox Control


KeyPressEventArgs


Parallel Arrays

Key Event Args


Dynamic Arrays

Redimension Array

Set Breakpoint

Start Debugger


ReDim Preserve


MultiDimensional Arrays

DataGridView Control


Length and Sort Methods


Structures

Pad Right

Split 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 Source


Configure Access Data Source


Add Product Class


Extract Local Database Data

Order PageLoad VB Code

Add New Web Page


Set Start Page

Display Cart Aspx Code

Display Cart Design View

Sorted List Definition

VB.NET Session State


Create CartItem Class

GetCartContents Function


Add To Cart Event Handler

Remove Cart Item Event


Clear Cart Event Handler


Can't open VisitorHistory.txt for reading