Hey all,

I am making a website for a project which allows the user to "purchase" items. Currently, I have all the items displayed in a grid view which has selection enabled.

When you click this, it stores the cell values into sessions which I retreive on another page as a label (my basket, if you will). What I would like to know is how to go further than just the 1 item, so I click my "Continue Shopping" button and it goes back to my shopping page where I can select another item which will again redirect to basket page and add it along side first item?

I have it working with 1 item at the moment, but when I go back and click another it overwrites the initial session.

Here is the code from my shopping page:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["CoffeeID"] = GridView1.SelectedRow.Cells[0].Text;
            Session["CoffeeName"] = GridView1.SelectedRow.Cells[1].Text;
            Session["CoffeePrice"] = GridView1.SelectedRow.Cells[2].Text;
            Session["CoffeeStrength"] = GridView1.SelectedRow.Cells[3].Text;
            Session["Origin"] = GridView1.SelectedRow.Cells[4].Text;
            Session.Timeout = 20;
            Response.Redirect("MoreDetails.aspx");
        }

And this from the basket page:

protected void Page_Load(object sender, EventArgs e)
        {
            lblID.Text = Session["CoffeeID"].ToString();
            lblName.Text = Session["CoffeeName"].ToString();
            lblPrice.Text = Session["CoffeePrice"].ToString();
            lblStr.Text = Session["CoffeeStrength"].ToString();
            lblOrigin.Text = Session["Origin"].ToString();
        }

What you could do is have one session to store the amount of items in the basket and then have the item sessions numbered, possibly like this:
Session["CoffeeID" + number] = GridView1.SelectedRow.Cells[0].Text;
Session["CoffeeName" + number] = GridView1.SelectedRow.Cells[1].Text;
Session["CoffeePrice" + number] = GridView1.SelectedRow.Cells[2].Text;
Session["CoffeeStrength" + number] = GridView1.SelectedRow.Cells[3].Text;
Session["Origin" + number] = GridView1.SelectedRow.Cells[4].Text;

And then use a for-loop like this:

for(int i = 0; i < Session["items"]; i++)
{
//Do whatever
}

It's up to you :)

Would that work for displaying the other item also?

What it currently does is output the data onto another form (moredetails.aspx), so if I was to hit the continue shopping button and choose a different item, the previous item will still be there and the new item will be next to it.

Then if I press the Checkout button, it will go to another page (say, checkout.aspx) which will carry over the "basket" items and have a label displaying the total price (item1 price * quantity)+(item2 price * quantity) for example.

Or would it be easier to include a check box in my grid view and have a button that adds all ticked rows to a cart session, then another button that goes to checkout page?

Would that work for displaying the other item also?

What it currently does is output the data onto another form (moredetails.aspx), so if I was to hit the continue shopping button and choose a different item, the previous item will still be there and the new item will be next to it.

Then if I press the Checkout button, it will go to another page (say, checkout.aspx) which will carry over the "basket" items and have a label displaying the total price (item1 price * quantity)+(item2 price * quantity) for example.

Yes, that would work, since it would store all the items in different sessions.

This article has been dead for over six months. Start a new discussion instead.