![cookies cookies]()
本文介绍了如何在单个cookie中写入和读取来自arraylist([0],[1]等)的两行以上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我需要一组值来显示网格视图。所以我在我创建的属性类中创建了两个类叫做属性和classname1 获取描述,idval和单个价格的设置属性 in classname1,我创建了一个名为arrylistinfo的arraylist。 arraylistobject是arrylistinfo的对象。 现在我将所有属性值添加到arraylist列表然后我绑定到gridview。 并且,我保存到下面的cookie中。 HttpCookie aCookie =新的HttpCookie(userInfo); foreach( classname1.arraylistobject中的属性b) { aCookie [cook_description] = b.Descript ; aCookie [cook_id] = b.Idval; aCookie [cook_price] = b.singleprice.ToString(); Response.Cookies.Add(aCookie); Response.Cookies [userInfo]。Expires = DateTime.Now.AddMonths(2); } classname1 ordinfocook = new classname1(); if(Request.Cookies [userInfo]!= null) { 属性attri = new Attributes(); if(Request.Cookies [userInfo]。HasKeys) { attri.Descript = Reques t.Cookies [userInfo] [cook_description]; attri.Idval = Request.Cookies [userInfo] [cook_id]; attri。 Singleprice = Convert.ToDecimal(Request.Cookies [userInfo] [cook_price]); ordinfocook.arraylistobject.Add(attri); GridView1.DataSource = ordinfocook.al; GridView1.DataBind(); } } 直到现在一切正常。但是这只适用于网格视图中的一行作为arralylist,因为[0]将具有describe,idval和single price。 但是,如何做两个以上行以及如何从cookies读取和写入? 因为我试图找到我的厨师使用 string [] cookies = Request.Cookies .AllKeys; 计算所有键。如何找到我的厨师的数量。当arraylist有两行以上时,如何做两行以上([0] = describe,idval,single price。[1] = descript,idval,single price等)... 需要帮助。I need set of values to show grid view. so i created two class called attributes and classname1in the attribute class i created get set properties for descript,idval and single price in the classname1 , i created arraylist called arrylistinfo. the arraylistobject is object of arrylistinfo.now i added all attributed values into arraylist list then i binded into gridview.And, i saved into cookies like below.HttpCookie aCookie = new HttpCookie("userInfo");foreach (Attributes b in classname1.arraylistobject){aCookie["cook_description"] = b.Descript;aCookie["cook_id"] = b.Idval;aCookie["cook_price"] = b.singleprice.ToString();Response.Cookies.Add(aCookie);Response.Cookies["userInfo"].Expires = DateTime.Now.AddMonths(2);}classname1 ordinfocook = new classname1();if (Request.Cookies["userInfo"] != null){Attributes attri = new Attributes();if (Request.Cookies["userInfo"].HasKeys){attri.Descript = Request.Cookies["userInfo"]["cook_description"];attri.Idval = Request.Cookies["userInfo"]["cook_id"];attri.Singleprice = Convert.ToDecimal(Request.Cookies["userInfo"]["cook_price"]);ordinfocook.arraylistobject.Add(attri);GridView1.DataSource = ordinfocook.al;GridView1.DataBind();}}Till noweverything works fine. But this works only for one rows in grid view as arralylist as [0] will have descript,idval and single price.But , How to do for more than two rows and how read and write from cookies ?Becuase when i try to find my cookes using string[] cookies = Request.Cookies.AllKeys;it comes the count all keys. how to find my cookes's count. how to do more than two rows when arraylist has more than two rows([0]=descript,idval,single price. [1]=descript,idval,single price and so on)...help needed.推荐答案 public class Person { public Person(int id, string name, string descr) { ID = id; Name = name; Description = descr; } public int ID { get; set; } public string Name { get; set; } public string Description { get; set; } } 现在代码将多个人存储到cookie并检索它们再次出现在List中,可以根据你的问题按照你的意愿提供网格视图。 Now the code which will store multiple persons to cookie and to retrieve them again to an List which can be used to feed the grid view as you wanted as per your question.protected void Page_Load(object sender, EventArgs e) { const string cookieName = "PersonInfo_";// Cookie name to be concated for all Person cookie // Prepare person arraylist with some sample data ArrayList sourcePersonList = new ArrayList(); sourcePersonList.Add(new Person(1, "name1", "description1")); sourcePersonList.Add(new Person(2, "name2", "description2")); sourcePersonList.Add(new Person(3, "name3", "description3")); // Add all the person list data to cookie with dfferent key name for all by concatinating primary key value to cookie name foreach (Person p in sourcePersonList) { HttpCookie personCookie = new HttpCookie(cookieName + p.ID); // Concat the primary key to the each cookie you created for each person personCookie["ID"] = p.ID.ToString(); personCookie["Name"] = p.Name; personCookie["Description"] = p.Description; Response.Cookies.Add(personCookie); Response.Cookies[cookieName + p.ID].Expires = DateTime.Now.AddMonths(2); } // Now filter the cookie names which are of your use and holds Person information only var cookieList = Request.Cookies.AllKeys.Where(s => s.Contains("PersonInfo_")); List<person> destPersonList = new List<person>(); // Get the values from cookie to a PersonList foreach (string cooki in cookieList) { if (Request.Cookies[cooki] != null) { Person p = new Person(Convert.ToInt32(Request.Cookies[cooki]["ID"]) , Request.Cookies[cooki]["Name"] , Request.Cookies[cooki]["Description"]); destPersonList.Add(p); } } // Now the variable 'destPersonList' having all the retrieved persons from cookies // GridView1.DataSource = destPersonList; // GridView1.DataBind(); }</person></person> b $ b 希望它会有所帮助。Hope it will be helpful. 这篇关于如何在单个cookie中写入和读取来自arraylist([0],[1]等)的两行以上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-29 21:17