• 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:

Cookie.aspx:

<%@ 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">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
        <tr>
            <td> Product Name:</td>
            <td>
                <asp:TextBox ID="txtproductname" runat="server"></asp:TextBox></td>
            </tr>
        <tr>
            <td>Quantity:</td>
            <td>
                <asp:TextBox ID="txtqunatity" runat="server"></asp:TextBox></td>
           
        </tr>
         <tr>
            <td colspan="2">
                <asp:Button ID="btncalculate" runat="server"
 
                Text="Calculate" OnClick="btncalculate_Click" /></td>
           
           
        </tr>
 
        <tr>
            <td> Product Name: </td>
            <td><asp:Label ID="lblproductname" runat="server"
           
          Text="Label" style="font-weight: 700"></asp:Label></td>
            </tr>
        <tr>
            <td>
               Quantity:
                </td>
            <td><asp:Label ID="lblquantity" runat="server"
 
            Text="Label" style="font-weight: 700"></asp:Label></td>
           
      </tr>
           <tr>
            <td>Total Amount: </td>
            <td><asp:Label ID="lbltotalamount" runat="server"
          
            Text="Label" style="font-weight: 700"></asp:Label> </td>
              
           
        </tr>
    </table>
    </div>
    </form>
</body>
</html>

 

Cookie.aspx.cs:

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);
                    Response.SetCookie(cookie);
                }
            }
        }
 
        protected void btncalculate_Click(object sender, EventArgs e)
        {
            lblproductname.Text =txtproductname.Text;
            lblquantity.Text =txtqunatity.Text;
            var Totalamount = Convert.ToDecimal(lblquantity.Text) *                    
    
                Convert.ToDecimal(Request.Cookies["rate"].Value);
            lbltotalamount.Text =Totalamount.ToString();
        }
    }
}

 

Output:


I hope this article will help to you.

1 comments

  1. good
    8/12/2017 6:10:14 AM

    good



Leave a comment

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

Welcome to SQL Tutorials Point

Don't have an account? Click here to Create a new account, it takes less than a minute.