UncleCoder.com

UncleCoder.com

Free programming examples and instructions

Asp.net Adding values to Cookie

Code and demo for how to add values to cookies in Asp.net

by Athil


Posted on 11 Mar 2017 Category: Asp.net Views: 1608

Edited on 03 Jun 2017


DEMO

 

A cookie is a small piece of text that are stored in client side browsers' memory. Here I am going to show how to create a cookie and add values in the cookie.

Creating and adding values to cookie

On Client side

   <form id="form1" runat="server">
   <div>    
       <h1> Add to cookie Demo </h1>
       <h2> User Info </h2>
       Name :   <asp:TextBox ID="txtName" runat="server"></asp:TextBox> <br>
       Age :    <asp:TextBox ID="txtAge" runat="server"></asp:TextBox> <br>
       Address Line 1: <asp:TextBox ID="TxtAddress" runat="server"></asp:TextBox> <br>
       Address Line 2: <asp:TextBox ID="TxtAddress2" runat="server"></asp:TextBox>
       <br />
      <asp:Button ID="BtnAddToCookie" runat="server" Text="Add to cookie" OnClick="BtnAddToCookie_Click" />
    </div>
    </form>

On Server Side

 protected void BtnAddToCookie_Click(object sender, EventArgs e)
        {
            HttpCookie userinfo = new HttpCookie("userInfoDemo");
            userinfo["Name"] =  txtName.Text;
            userinfo["Age"] = txtAge.Text;
            userinfo["Addrerss1"] = TxtAddress.Text;
            userinfo["AddressLine2"] = TxtAddress2.Text;
            userinfo.Expires = DateTime.Now.AddDays(10); // set expire time
            Response.Cookies.Add(userinfo);
            Response.Write("value added in cookie");
        }

Screen Shot

Here I added expire time of cookie to 10 days, we can change it in the code as we need.

We can view the values in cookie using Developer Tool. To view value in cookie, Follow the instructions. (Google Chrome)

1. Press f12 from the site anywhere to open developer tool.

2. Select application from the tab.

3  Select cookie from the left hand side.

 

Screen Shot

 

FULL CODE

 

On Client Side

 

  <form id="form1" runat="server">
    <div>
    
 
        
        <h1> &nbsp;</h1>
        <h1> Add to cookie Demo </h1>
       <h2> User Info </h2>
     Name :   <asp:TextBox ID="txtName" runat="server"></asp:TextBox> <br>
     Age :    <asp:TextBox ID="txtAge" runat="server"></asp:TextBox> <br>
     Address Line 1: <asp:TextBox ID="TxtAddress" runat="server"></asp:TextBox> <br>
    Address Line 2: <asp:TextBox ID="TxtAddress2" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="BtnAddToCookie" runat="server" Text="Add to cookie" OnClick="BtnAddToCookie_Click" />

        <br />

<h1> Taking values from cookies </h1>

        <asp:Button ID="btnRetrieve" runat="server" Text="Retrieve values from cookies" OnClick="btnRetrieve_Click" /> <br/>
          Name :   <asp:TextBox ID="txtnameretrieve" runat="server"></asp:TextBox> <br/>
     Age :    <asp:TextBox ID="txtageretrieve" runat="server"></asp:TextBox> <br/>
     Address Line 1: <asp:TextBox ID="txtaddr1retrieve" runat="server"></asp:TextBox> <br/>
    Address Line 2: <asp:TextBox ID="txtaddr2retrieve" runat="server"></asp:TextBox>
    
        <br />
    <h1> Clear Cookies </h1>

        <asp:Button ID="btnClearCookies" runat="server" Text="Clear Cookies" OnClick="btnClearCookies_Click" />
    </div>

    </form>

 

On Server Side

 protected void BtnAddToCookie_Click(object sender, EventArgs e)
        {
            HttpCookie userinfo = new HttpCookie("userInfoDemo");
            userinfo["Name"] =  txtName.Text;
            userinfo["Age"] = txtAge.Text;
            userinfo["Addrerss1"] = TxtAddress.Text;
            userinfo["AddressLine2"] = TxtAddress2.Text;
            userinfo.Expires = DateTime.Now.AddDays(10);
            Response.Cookies.Add(userinfo);
            Response.Write("value added in cookie");
        }

        protected void btnRetrieve_Click(object sender, EventArgs e)
        {
            if (Request.Cookies["userInfoDemo"] != null)
            {
                HttpCookie cookiename = Request.Cookies["userInfoDemo"];
                txtnameretrieve.Text = cookiename["Name"].ToString();
                txtageretrieve.Text =  cookiename["Age"].ToString(); 
                txtaddr1retrieve.Text =   cookiename["Addrerss1"].ToString();
                txtaddr2retrieve.Text =  cookiename["AddressLine2"].ToString();
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, typeof(Page), "ScriptName", "<script>alert('No cookies in this name');</script>", false);
            }
        }

        protected void btnClearCookies_Click(object sender, EventArgs e)
        {
            HttpCookie cookiename = Request.Cookies["userInfoDemo"];
            cookiename.Expires = DateTime.Now.AddDays(-1);
            ScriptManager.RegisterStartupScript(this, typeof(Page), "ScriptName", "<script>alert('Cookie Cleared');</script>", false);
        }

Related Post

Taking Values from Cookies Asp.net
Cookies in javascript

 



Leave a Comment:


Advertisement