Compare list : cookie not refresh??

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
15 years ago
Hi,

I'm trying to improve the compare list, it's almost complete but there is something wrong with the cookie which is blocking me.

I guess a picture will explain the context better than me :

http://img509.imageshack.us/img509/1092/comparelist.th.jpg

The dropDownLists contains the category product list of the product chosen. When I select another product in the dropdownlist,
the selected product must replace the product previously chosen.

Here is the code for my dropdownlist :

DropDownList drpProductList = new DropDownList();
drpProductList.DataSource = productCollection;
drpProductList.DataTextField = "Name";
drpProductList.DataValueField = "ProductID";
drpProductList.DataBind();
drpProductList.ID = "drpProductList" + product.ProductID.ToString();
drpProductList.SelectedIndexChanged += new EventHandler(this.drpProductList_SelectedIndexChanged);
drpProductList.AutoPostBack = true;
drpProductList.Attributes.Add("CommandArgument", product.ProductID.ToString());
headerCellDiv.Controls.Add(drpProductList);


Here is my drpProductList_SelectedIndexChanged function :

        private void drpProductList_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Supprimer le productID qui est dans le CommandArgument du controle de la comparelist
            //pour y ajouter le product qu'on vient de sélectionner
            DropDownList drp = (DropDownList)sender;
            int productIDOld = Convert.ToInt32(drp.Attributes["CommandArgument"]);
            int productIDNew = Convert.ToInt32(drp.SelectedValue);
            Product productNew = ProductManager.GetByProductID(productIDNew);

            ProductManager.RemoveProductFromCompareList(productIDOld);
            //ProductCollection compareProducts = ProductManager.GetCompareProducts();
            
            ProductManager.AddProductToCompareList(productIDNew);
            //compareProducts = ProductManager.GetCompareProducts();

            //int index = compareProducts.FindIndex(delegate(Product p) { return p.ProductID ==  productIDOld ; });
            //compareProducts[index] = productNew;
            Response.Redirect("~/CompareProducts.aspx");

        }


Everything works fine, I have no error, but the result is like the line :
ProductManager.RemoveProductFromCompareList(productIDOld);

is totally ignored. The old product is never removed, however the execution in the function RemoveProductFromCompareList is OK.

It's like the cookie taken is the old one and not the new one.

I really don't know what to do.

Can you help me please?
15 years ago
Hi,

Finally, I find the problem. I just forgot that cookies are on client side and not on server side, so if I don't  go on client side to update the cookie, It definitely can't work.

So I create a new function in the ProductManager which group the add and the remove functions.

If it can help someone :
        public static void AddRemoveProductToCompareList(int ProductIDOld, int ProductIDNew)
        {
            List<int> oldProductIDs = GetCompareProductsIDs();

            List<int> newProductIDs = new List<int>();
            newProductIDs.AddRange(oldProductIDs);
            newProductIDs.Remove(ProductIDOld);
            newProductIDs.Add(ProductIDNew);


            HttpCookie compareCookie = HttpContext.Current.Request.Cookies.Get("NopCommerce.CompareProducts");
            if (compareCookie == null)
                compareCookie = new HttpCookie("NopCommerce.CompareProducts");
            compareCookie.Values.Clear();
            int maxProducts = 5;
            int i = 1;
            foreach (int newProductID in newProductIDs)
            {
                compareCookie.Values.Add("CompareProductIDs", newProductID.ToString());
                if (i == maxProducts)
                    break;
                i++;
            }
            compareCookie.Expires = DateTime.Now.AddDays(1.0);
            HttpContext.Current.Response.Cookies.Set(compareCookie);
        }


Happily it's the end of the week...
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.