且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从Arraylist中保存和检索信息

更新时间:2023-01-11 23:33:19

尝试类似



列表< 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())
{
会话[ product] = product;
string url = CompareProducts.aspx 跨度>;
Response.Redirect(url);
}





在您的目标页面上;



List< string> products = null ;
if (会话[ product]!= null
{
products =(List< string>)会话[ product];
}


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


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

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
               }


           }

Try something like

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;

List<string> products = null;
if (Session["product"] != null)
{
    products = (List<string>)Session["product"];
}