Results 1 to 12 of 12

Thread: [2005] Ideas for shopping basket, online store.

  1. #1

    Thread Starter
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    [2005] Ideas for shopping basket, online store.

    Hey there.
    Ive just started out with ASP.NET and web development in general.

    I'm trying to make an online store.
    I've got a database of store items available for purchase and I list them on my site, now I need to implement some sort of shopping basket where whatever the customer chooses to purchase is placed.
    I suppose what would be interesting to store is the items ID and quantity? But where and how?

    This sounds like a trivial thing, but I'm totally lost with what I can and can not do in ASP.NET.

    The customers are not required to sign in, and should be able to start shopping right away.

    Any ideas?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  2. #2
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: [2005] Ideas for shopping basket, online store.

    In the past, I've created a table to use as a cart. Basically storing the ItemID, QTY and Price along with a GUID. I would create the GUID once the customer first accesses the store and would throw it inside a cookie that the store would read everytime a customer hits the site.

    This way you will store minimal information in the local machine's cookie and have something to identify the customer in your database.

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] Ideas for shopping basket, online store.

    Depending on whether you want the cart items to be available to the user even if he goes away for a day and comes back or if you want the cart items to be available just for as long as the user is browsing around, there are two ways - cookie or session.

    A cookie lets you specify how long it can sit on the user's machine. This means that if the user browses your site, picks up a few items into his cart, then goes away for a week and comes back, the website could still remember what items were chosen. That's of course if you make the cookie expire much later than that.

    The other way is a session variable. The session variable can hold a custom type if you'd like, one that holds the item id and the quantity. If the users goes away and comes back, the cart will be empty as the session variable will be empty too.

  4. #4

    Thread Starter
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Ideas for shopping basket, online store.

    Thanks alot guys.
    Ive decided to go with the session variable. I have an idea that might or might not work... I havent tried it yet, (just got home from uni, where I'm working on it, so i cant do until tomorrow)...
    What if a BindingList<Product> was created as a session variable, would I then be able to bind it to a DropDownList? So I'd just have to pop new things into the session variable that is a BindingList and it'd update the contents DropDownList automatically Would it work you think?

    (The dropdown would be at the bottom of the master page, visible at all times).
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] Ideas for shopping basket, online store.

    If it's serializable, it can go into Session.

    Watch your naming, though. In this case, you'd probably need a list of CartItems rather than Products (but the CartItems could possibly have a Product property or ProductId property). Anyways, I mention it because as you progress, this could come back and bite you.

    Yes, it should work. One way to find out.

  6. #6

    Thread Starter
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Ideas for shopping basket, online store.

    Hmm, it looks like it should work alright, I'm setting the DropDowns DataSource and DataMember properties like this in the MasterPage's Load eventhandler:

    CSharp Code:
    1. public partial class MasterPage : System.Web.UI.MasterPage
    2. {
    3.     protected void Page_Load(object sender, EventArgs e)
    4.     {
    5.         this.DropDownList1.DataSource = Basket.getBasket(Session);
    6.         this.DropDownList1.DataMember = "Id";
    7.     }
    8. }

    Basket.getBasket is a method that returns a BindingList<Product> stored as a sessionvariable in the session passed to it (if it doesnt exist yet it also creates it).

    When I make purchases i verify that the BindingList<Product> stored in the session variable is updated, but the DropDownList isnt updated, its still showing up as empty.

    Is there anything else I must do to make this work?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  7. #7

    Thread Starter
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Ideas for shopping basket, online store.

    Just found out that if i add
    CSharp Code:
    1. this.DropDownList1.DataBind();
    after setting the DataSource and DataMember, it kinda works.

    The problem now is, everytime I click the "Buy" link for a certain product, the Master page apparently reload, thus running its Load eventhandler (the one posted above) again.
    This all happends before the product is actually placed in the basket, so the DropDownList doesnt show the purchase until after I click buy another time on any product...it then displays the updated basket except for the most recent product..
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  8. #8

    Thread Starter
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Ideas for shopping basket, online store.

    Alright since my problem seems to lie in the fact that the MasterPage's Load eventhandler runs before an item is placed in the shopping basket, so I've created what I think looks like a dirty hack...I created this method in the MasterPage code:

    CSharp Code:
    1. public void updateBasketList()
    2.     {
    3.         this.DropDownList1.Items.Clear();
    4.         this.DropDownList1.DataSource = Basket.getBasket(Session);
    5.         this.DropDownList1.DataBind();
    6.     }

    And I do this in the eventhandler for the "Buy" LinkButtons on the other page:
    CSharp Code:
    1. public void addToBasket(object sender, System.EventArgs e)
    2.     {
    3.         LinkButton clickedButton = (LinkButton)sender;
    4.         Basket.addToBasket(Session, int.Parse(clickedButton.CommandArgument), 1);
    5.         MasterPage thisMaster = (MasterPage)this.Master;
    6.         thisMaster.updateBasketList();
    7.     }

    Atleast it works, but it gives me a couple of questions;

    1. I always thought that once you set a DataSource, the control would update automatically each time the contents of the datasource is changed, still in my case I have to set the DataSource again everytime I know that it has changed. Could someone explain this?

    2. I also needed to clear the items from the DropDownList each time before I set the DataSource, or else it'd keep old items, this is also something I thought binding a control to a datasource would handle. Was I wrong in thinking that?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  9. #9
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] Ideas for shopping basket, online store.

    Quote Originally Posted by Atheist
    Just found out that if i add
    CSharp Code:
    1. this.DropDownList1.DataBind();
    after setting the DataSource and DataMember, it kinda works.

    The problem now is, everytime I click the "Buy" link for a certain product, the Master page apparently reload, thus running its Load eventhandler (the one posted above) again.
    This all happends before the product is actually placed in the basket, so the DropDownList doesnt show the purchase until after I click buy another time on any product...it then displays the updated basket except for the most recent product..
    Surround that code (which keeps getting executed) in a Page.IsPostBack check. It checks if it is a first-time load (which is when you want it to run) or if it's an event such as a button click. You want that to run if it's not a page.ispostback.

  10. #10
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] Ideas for shopping basket, online store.

    Quote Originally Posted by Atheist

    1. I always thought that once you set a DataSource, the control would update automatically each time the contents of the datasource is changed, still in my case I have to set the DataSource again everytime I know that it has changed. Could someone explain this?

    2. I also needed to clear the items from the DropDownList each time before I set the DataSource, or else it'd keep old items, this is also something I thought binding a control to a datasource would handle. Was I wrong in thinking that?
    The answer to both questions are related - the control maintains viewstate and holds the items in there. So you fill a dropdownlist and it'll hold on to those values until you say otherwise. That's also why the old items are in there. As for the specific answer to #1, it may be because you're not doing the Page.IsPostBack check.

  11. #11

    Thread Starter
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Ideas for shopping basket, online store.

    Thank you, I tried doing the postback thingie and indeed it only runs the code once, but the DropDownList still isnt updated when new items are added to the basket.
    You must excuse me but I'm kind of new to all this databinding, and its especially confusing when its not working the way I always have though it would work. What is the advantage of databinding if i manually have to reset the datasource and call databind every time i have made changes to my BindingList?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  12. #12
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] Ideas for shopping basket, online store.

    ASP.NET pages work a little differently from what you know in Windows forms. Winforms are an always-connected environment.

    ASP.NET is a disconnected environment. User makes a request to a page, it does something, gets data, creates HTML and sends that back, then the ASP.NET page and associated objects are destroyed.

    So it has no way of knowing that something has changed. You need to tell it if any changes occur in the data source.

    While it may seem like a disadvantage, the advantage is the reverse of what you said: you don't always have to make a database call to repopulate the gridview or datalist or whatever control you're using across postbacks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width