• Introduction:
  • In this article we will learn how to use Cookies in asp.net or what is Cookies in asp.net or why Cookies in asp.net with example.
  • Description:
        • Cookies is a small amount of data stored on the client machine for the website to use. This allows web application to save information for the user, and then re-use it on the page anywhere in your application if needed. ASP.NET makes it easy for us to both read and write cookies. Cookies may be used for authentication ,identification of a user session, user's preferences, shopping cart contents etc.  Cookies can also be used for transfering the data from one page to another.

Let’s see a small example of Cookies:


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="cookie.aspx.cs"
Inherits="AspDotNet.cookie" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <form id="form1" runat="server">
            <td> Product Name:</td>
                <asp:TextBox ID="txtproductname" runat="server"></asp:TextBox></td>
                <asp:TextBox ID="txtqunatity" runat="server"></asp:TextBox></td>
            <td colspan="2">
                <asp:Button ID="btncalculate" runat="server"
                Text="Calculate" OnClick="btncalculate_Click" /></td>
            <td> Product Name: </td>
            <td><asp:Label ID="lblproductname" runat="server"
          Text="Label" style="font-weight: 700"></asp:Label></td>
            <td><asp:Label ID="lblquantity" runat="server"
            Text="Label" style="font-weight: 700"></asp:Label></td>
            <td>Total Amount: </td>
            <td><asp:Label ID="lbltotalamount" runat="server"
            Text="Label" style="font-weight: 700"></asp:Label> </td>



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace AspDotNet
    public partial class cookie : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)
            if (!IsPostBack)
                if (Request.Cookies["rate"] == null)
                    HttpCookie cookie = new HttpCookie("rate");
                    cookie.Value = "200";
                    cookie.Expires = DateTime.Now.AddHours(1);
        protected void btncalculate_Click(object sender, EventArgs e)
            lblproductname.Text =txtproductname.Text;
            lblquantity.Text =txtqunatity.Text;
            var Totalamount = Convert.ToDecimal(lblquantity.Text) *                    
            lbltotalamount.Text =Totalamount.ToString();



How to use Cookies in Asp.Net

I hope this article will help to you.


  1. 8/12/2017 6:10:00 AM


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

You may also like