Click here to Skip to main content
15,887,992 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: (untagged)
Currently, what am doing is basically displaying product caatalog in my website. there is a checkbox button above every product. let say i want to select 3 checkboxes and click on compare button, it will lead me to another page. I am trying to save the data in array list, however, i can't


C#
void GetCheckedBox()
   {


       foreach (DataListItem item in DataList1.Items)
       {
           HtmlInputCheckBox cb = item.FindControl("FavChkBox") as HtmlInputCheckBox;
           if (cb != null && cb.Checked)
           {

               selectedProducts = selectedProducts + "," + cb.Value;


               LblText.Text = selectedProducts;
               Product.Add(selectedProducts);


               LblText.Text = selectedProducts;
               Session["product"] = Product;
             string url ="CompareProducts.aspx";
            Response.Redirect(url);
           }



       }
   }



.CompareProducts.aspx.cs
C#
protected void Page_Load(object sender, EventArgs e)
   {

           if (!IsPostBack)
           {


               ArrayList Product = Session["product"] as ArrayList;

             // string selectedProduct = Product.ToString();
              GridView1.DataSource = Product;
             GridView1.DataBind();
                   //array list is not null, safe to use
               }


           }
Posted
Comments
[no name] 4-Aug-15 12:06pm    
Why are you using an ArrayList to begin with? And what does "trying to save the data in array list, however, i can't" mean?
Member 11829433 4-Aug-15 12:10pm    
I am not sure whether if my arraylist will stock the information whenever i checked on 3 products
Sergey Alexandrovich Kryukov 4-Aug-15 12:30pm    
First, tag your language.
Never use ArrayList, it's obsolete, use System.Collections.Generic.List.
—SA

1 solution

Try something like

C#
List<string> products = new List<string>();

foreach (DataListItem item in DataList1.Items)
{
    HtmlInputCheckBox cb = item.FindControl("FavChkBox") as HtmlInputCheckBox;

    if (cb != null && cb.Checked)
    {

        products.Add(cb.Value);

    }

}

if (products.Any())
{
    Session["product"] = product;
    string url ="CompareProducts.aspx";
    Response.Redirect(url);
}


On your target page;

C#
List<string> products = null;
if (Session["product"] != null)
{
    products = (List<string>)Session["product"];
}
 
Share this answer
 
v3
Comments
Richard Deeming 4-Aug-15 13:41pm    
You can simplify the target page code to:
List<string> products = Session["product"] as List<string>;
F-ES Sitecore 4-Aug-15 14:22pm    
You sure can, but it's good to get people into the habit of remembering that their session variables might not be there, and in this case he'd probably want to do something if there was no session variable like redirect somewhere. He could always check if products was null, but it makes it clearer what is going on if the "work done" is as a result of the variable not existing, than if an as cast failing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900