I have written the piece of code below to get a productID from a gridView when the user clicks on the select link.
Convert.ToInt32(GridView1.SelectedDataKey.Values["productID"])))
However, if a user clicks on this more than one click the newer value replaces the previous. Is there a way to keep adding to the cart list when the user clicks on a new item?
Hope that makes sense
Thanks
Edit:
Here's my code for the Shopping Page:
public partial class _Default : System.Web.UI.Page
{
    List<BasketClass> cart = new List<BasketClass>();
    protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
    {
        cart.Add(new BasketClass(Convert.ToInt32(GridView1.SelectedDataKey.Values["BookID"])));
        Session.Add("CartSess", cart);
        Response.Redirect("Basket.aspx");
    }
}
I dont know if the location of creating the list is important? Wasn't sure if it was placed in the click event if it would keep creating a new instance?
Then fo开发者_C百科r the Basket Page I have:
protected void Page_Init(object sender, EventArgs e)
{
    List<BasketClass> cart = (List<BasketClass>)Session["CartSess"];
    foreach (BasketClass BookID in cart)
    {     
        GridView1.DataSource = cart;
        GridView1.DataBind();
        AccessDataSource1.SelectCommand = "SELECT [BookID], [book_title] FROM [tblBook] ";                     
    }
}
You should not create BasketCart list object in the top of page, even if you do that then there is no very much significant difference, i think you cna do this:
protected void Page_Init(object sender, EventArgs e)
{    
    if(Session["CartSess"]!=null)
    {    
       foreach (BasketClass BookID in (List<BasketClass>)Session["CartSess"])
       {     
         GridView1.DataSource = cart;
         GridView1.DataBind();
        AccessDataSource1.SelectCommand = "SELECT [BookID], [book_title] FROM [tblBook] ";                     
       }
    }
}
and your GridView even should be:
protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
{
   List<BasketClass> cart;
   if(Session["CartSess"]!=null)
   { 
       cart = (List<BasketClass>)Session["CartSess"]
   }
   else
     cart = new List<BasketClass>();
   cart.Add(new BasketClass(Convert.ToInt32(GridView1.SelectedDataKey.Values["BookID"])));
   Session.Add("CartSess", cart);
   Response.Redirect("Basket.aspx");
}
It is not this line that is causing your issue, it is where you are storing it. Are you passing this value into a collection?
use a List<int> persisted in ViewState?
public List<int> Selected
{
    set { ViewState["_selected"] = value; }
    get
    {
        if(ViewState["_selected"] == null)
            return new List<int>();
        return (List<int>)ViewState["_selected"];
    }    
}
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论