locked
How to create and show multiple cookies in GridView in asp.net c# RRS feed

  • Question

  • Hi all, I am working on Online shopping website where end user can select multiple items at a time and i need to show them in Cart-item where he has selected his choice items. I want to do this by Cookies in that As user clicks on Add-To-Cart button it gets stored in cookies and in the Show-My-Cart-Item page it shows all the selected items in GridView format. 

    Kindly guide me how to do this coding. Suppose a scenario of ItemId,ItemName,Price.

    Anyhow I got following code:

    <div>
        <asp:DropDownList ID="ddlCookies" runat="server"></asp:DropDownList>
            <br />
            <asp:Button ID="btnSave" runat="server" Text="Save Cookie" OnClick="btnSave_Click" />
        </div>
            <div >
                <asp:Label ID="lblCookies" runat="server"></asp:Label>
            </div>
            <div style="background-color:chocolate">
                <asp:GridView ID="grdCookies" runat="server"></asp:GridView>
            </div>

    protected void Page_Load(object sender, EventArgs e)
        {
            Response.Cache.SetExpires(DateTime.Now);
            //lblCookies.Text = "My cookie value is "+Request.Cookies["Background"].Value;
            if (!Page.IsPostBack)
            {
                ddlCookies.Items.Add("A");
                ddlCookies.Items.Add("B");
                ddlCookies.Items.Add("C");
                ddlCookies.Items.Add("D");
                showMyCookies();
            }
        }
        private void showMyCookies()
        {
    
            string fname=Request.Cookies["Background"]["FirstName"];
            string lname = Request.Cookies["Background"]["LastName"];
            ArrayList colCookies = new ArrayList();
            for (int i = 0; i < Request.Cookies.Count; i++)
            {
    
                colCookies.Add(Request.Cookies[i]);
                grdCookies.DataSource = colCookies;
                grdCookies.DataBind();
            }
        }
       
    
        protected void btnSave_Click(object sender, EventArgs e)
        {
            HttpCookie myCookie = new HttpCookie("Background");
            myCookie.Value = ddlCookies.SelectedItem.Text;
            myCookie.Values.Add("FirstName","Mayank");
            myCookie.Values.Add("LastName","Pandey");
            //Response.Cookies.Add(new HttpCookie("Cookie1","Value1"));
            myCookie.Expires.AddYears(100);
            showMyCookies();
        }
    But I am unable to get all the cookies values except DropDownList Value. And in gridView I get only last ddl value. Though I have used foreach loop But it does not work. Kindly guide me how get all the records in gridview.
    It will be appreciable if you guide me with other examples.


    Mayank Mani Pandey India


    Tuesday, July 14, 2015 7:58 AM

Answers

  • asp.net forum is the right forum for your question, here is for C# coding issues in specific.

    Fouad Roumieh

    • Proposed as answer by Kristin Xie Wednesday, July 15, 2015 3:10 AM
    • Marked as answer by Just Karl Friday, July 31, 2015 4:51 PM
    Tuesday, July 14, 2015 9:57 AM