UncleCoder.com

UncleCoder.com

Free programming examples and instructions

Taking Values from Cookies Asp.net

code and demo for how to retrieve values from cookies in asp.net

by Athil


Posted on 19 Mar 2017 Category: Asp.net Views: 727

Edited on 10 Aug 2017



DEMO

A cookie is a small piece of text that are stored in client browser memory. We can view the values of cookie using developer tools of browsers.  Here I am going to show how to check a cookie existed, contains value and how to retrieve value from cookies in asp.net using c#. 

Check cookie existed

if (Request.Cookies["cookiename"] != null)
{ 
        //Cookie Existed
}

 

Taking Values From Cookies

if (Request.Cookies["cookiename"] != null)
{
         HttpCookie cookiename= Request.Cookies["cookiename"];
         string value1= cookiename["value1"].ToString();
         string value2= cookiename["value2"].ToString();
}

Set Timeout of cookie

HttpCookie cookiename= Request.Cookies["cookiename"];
cookiename.Expires = DateTime.Now.AddDays(10);

Clear cookie

 HttpCookie cookiename= Request.Cookies["cookiename"];
 cookiename.Expires = DateTime.Now.AddDays(-1);

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 Posts

Asp.net Adding values to Cookie

Cookies in javascript

 



Leave a Comment:


Advertisement