I'm creating a shopping basket in ASP.NET using session variables to pass the data from a shopping.aspx page to basket.aspx, currently I have the pages passing the primary key of the product with a gridview on the basket.aspx used to display the data from the database.
开发者_运维技巧However this only works for one item at a time, how can I extended the session variable so multiple products can be added, as well as quantities etc?
You can use your own object, eg. Basket which can have one or more properties.
Object should be market as Serializable.
For example:
[Serializable]
public class Basket
{
public List<BasketItem> Items {get;set;}
public int UserId {get;set;}
}
[Serializable]
public class BasketItem
{
//...
}
You can put (almost) any object into the session, not just strings. So you could use a List<string>
for a list of keys, or even a List<Product>
.
EDIT
So in the first page you would get
var bookids = new List<string>();
// collect all book IDs into the 'bookids' list
Session["bookIDs"] = bookids;
and in the second page:
var bookids = Session["bookIDs"] as List<string>;
// use all IDs
精彩评论