p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Go Back   p2p.wrox.com Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.1
I forgot my password Register Now
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.

 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 18th, 2004, 05:19 PM
Authorized User
Points: 39, Level: 1
Points: 39, Level: 1 Points: 39, Level: 1 Points: 39, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2004
Location: , , .
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



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
  #2 (permalink)  
Old March 21st, 2004, 05:34 PM
planoie's Avatar
Friend of Wrox
Points: 16,368, Level: 55
Points: 16,368, Level: 55 Points: 16,368, Level: 55 Points: 16,368, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,394
Thanks: 0
Thanked 2 Times in 2 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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
  #3 (permalink)  
Old March 22nd, 2004, 06:01 PM
Authorized User
Points: 39, Level: 1
Points: 39, Level: 1 Points: 39, Level: 1 Points: 39, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2004
Location: , , .
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:)

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
  #4 (permalink)  
Old March 22nd, 2004, 06:41 PM
Authorized User
Points: 39, Level: 1
Points: 39, Level: 1 Points: 39, Level: 1 Points: 39, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2004
Location: , , .
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;)

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
  #5 (permalink)  
Old March 23rd, 2004, 10:14 AM
planoie's Avatar
Friend of Wrox
Points: 16,368, Level: 55
Points: 16,368, Level: 55 Points: 16,368, Level: 55 Points: 16,368, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,394
Thanks: 0
Thanked 2 Times in 2 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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Please review open-source ASP.NET shopping cart nopCommerce ASP.NET 3.5 Professionals 0 October 31st, 2008 06:18 PM
shopping cart-fly to basket in asp.net shanthiniGanesh .NET Framework 2.0 1 December 18th, 2007 07:31 AM
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
Ch 11 Shopping Cart Using Sessions happyhorseygal BOOK: Beginning ASP.NET 1.1 3 November 23rd, 2004 06:08 PM



All times are GMT -4. The time now is 05:26 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc