Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.1
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 18th, 2004, 05:19 PM
Authorized User
 
Join Date: Mar 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Asp.net 1.1 with Vb.net Ch 11 Shopping Cart



Please bear with me if i ask stupid questions im a beginner :D i completed the shopping cart using sessions in chapter 11. im slightly confused by it!!
1.in order to add something to the shopping cart you have to click on the Buy it button twice??
2.Then when u want to add another item to the shopping cart you again have to click on it twice and then it adds one item on to the existing item you bought and then adds 1 item onto the new item!! Its Mad!! E.g u click twice on the buy a shirt button - it registers that u have one shirt in the cart, then u click twice on the buy the hat button and it registers that u have 3 shirts and one hat in the cart. ITS VER VERY STRANGE!!
i have downloaded the code off the website and it works the same i just want to able to click on the Buy button and that item to be added once to the cart. Ive messed around with the code for hours and hours and hours and guess what NO JOY!! Any help would be greatly appreciated
p.s ive posted the code below

 Sub Page_Prerender(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'this method is run before the page is displayed
        'and after all the other events have been processed

        'contents are displaye don the page by binding
        'by contents of the hashtable to the repeater

        Dim basketTable As System.Collections.Hashtable = Session("Basket")
        basketlist.DataSource = basketTable
        basketlist.DataBind()

        'If basketTable.Count = 0 Then
        'lblBasketMessage.Text = "nothing - please buy something!"
        'Else
        ' lblBasketMessage.Text = ""

        'End If

    End Sub
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'each time the page is loaded the session object is checks to see if it contains a basket. If it
        'doesnt an empty basket is created
        If Session("Basket") Is Nothing Then
            InitializeBasket()

        End If
    End Sub



    Sub btnEmptyBasket_Click(ByVal sender As Object, ByVal e As EventArgs)
        'clears out any existing basket data

        InitializeBasket()

    End Sub

    Sub AddItemToBasket(ByVal sender As Object, ByVal e As EventArgs)
        'all 3 item buttons call this method
        'each time this is run it gets the command argument of each property of the revelant button
        'the command arguement then populates the session object

        Dim itemName As String = sender.CommandArgument

        'a hashtable object tempoparily stores the contents of the sessions basket items.
        Dim basketTable As System.Collections.Hashtable = Session("Basket")

        'if the item has never been added to the session before add a new empty entry into the
        'hash table for this entry
        ' If basketTable(itemName) Is Nothing Then
        'basketTable(itemName) = 0
        'End If


        Dim itemCount As Integer = basketTable(itemName)
        basketTable(itemName) = itemCount + 1

    End Sub

    Sub InitializeBasket()
        'clear out the sesson objects and fill it with a new
        'empty has table
        Dim basketTable As New System.Collections.Hashtable
        Session("Basket") = basketTable

    End Sub



End Class



 
Old March 21st, 2004, 05:34 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

The problem is this:
You are binding the basket data BEFORE you add an item.

[u]Explanation</u>

You have the following method declaration:

Sub Page_Prerender(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

The key to the problem is the "Handles MyBase.Load". This tells this method to run when the page loads.

When ASP.NET processes events, they happen after the page is loaded. You are binding the basket data, and THEN the button click event handler (AddItemToBasket) is called. The basket data has already been generated before the item is added resulting in a delay in the data that you are seeing on the page.

Typically, the way this should work is something like the following.

When the page loads
- Prefill controls only on the FIRST page load (based on IsPostback)
You can do this because the controls will maintain their one data between postbacks. You don't need to prefill them every time.

Any event that affects the data that shows up in the controls:
- Modify the data based on the event that is being called (i.e. Add item to shopping basket)
- Update controls that reflect this output.

Here's how it would basically work for you:
Code:
Sub Page_Load(...)
    If Not IsPostback Then
        BindBasket()
    End If
End Sub

Sub AddItemToBasket(...)
    'TODO: Add the item to the basket here
    BindBasket()
End Sub

Sub BindBasket()
    'TODO: Load the basket data
    'TODO: Bind it to the basketlist
End Sub
Peter
------------------------------------------------------
Work smarter, not harder.
 
Old March 22nd, 2004, 06:01 PM
Authorized User
 
Join Date: Mar 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the help much appreciated:D but just one question? what was the point in the question in the book using the Page Prender in the first place for?? Anyhow going to give a go what u suggested:)

 
Old March 22nd, 2004, 06:41 PM
Authorized User
 
Join Date: Mar 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I Got it to work :) Are ya feeling my joy here!!! ok so i have a sesson working on one page but say i wanted the shopping cart e.g to follow through over a number of pages.. i.e a user chooses to buy an item on one page then views Merchandise pg2 and chooses to buy an item on that page too. So in essence what i mean is how do u get a session to follow through over a number of pages???? Any body any suggestions are GREATLY appreciated;)

 
Old March 23rd, 2004, 10:14 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

All you need to do is do what you are doing on this page to retrieve the session data.

       Dim basketTable As System.Collections.Hashtable = Session("Basket")

The point to using the session is to do exactly what you want to do, that is, to carry information between pages during the user's session. The session information is available to any page hit for that user and it applies only to that user.

When you have another page, like a basket summary page, then you use that line above to get the basket hash table from the session so you can use it to show all the items in the basket.

I'm not sure what was meant by the example because I am not familiar with that particular text. If that is some of the exact sample code, then I'm surprised. Usually, the handler is named something that matches the event that it's handling. The fact that the first method you posted is NAMED "Page_Prerender" is really irrelevant. The important part is what event it's actually handling. I would think that the code you posted would work as it is if you only changed that first method from handling MyBase.Load to MyBase.PreRender.

Peter
------------------------------------------------------
Work smarter, not harder.





Similar Threads
Thread Thread Starter Forum Replies Last Post
shopping cart-fly to basket in asp.net shanthiniGanesh .NET Framework 2.0 3 August 1st, 2012 01:36 PM
Please review open-source ASP.NET shopping cart nopCommerce ASP.NET 3.5 Professionals 0 October 31st, 2008 05:18 PM
Ch.13 Builing a Shopping Cart seannie BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 3 February 7th, 2007 10:11 AM
migrate shopping cart from asp to asp.net aranjan ASP.NET 1.0 and 1.1 Professional 1 February 5th, 2007 04:30 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.