UncleCoder.com

UncleCoder.com

Free programming examples and instructions

Adding values to cookies - Asp.net

Demo and code for how to add values to cookies in Asp.net

by Athil


Posted on 27 Jul 2018 Category: Asp.net Views: 146

Edited on 28 Jun 2019


Here I am going to show how to add values to cookies in Asp.net

DEMO

Cookies are the values that are stored in the users' web browser.

server side (c#) code for adding values to cookies.

            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");

 

Server side (C#) code to retrieve values from cookies

            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();
            }

 

Demo code

On Client side 

ASPX

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddtoCookie.aspx.cs" Inherits="recaptcha.AddtoCookie" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <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>

    
   

</body>
</html>

Server Side (c#)

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);
        }


Leave a Comment:


Advertisement