How to create Cookies in ASP.NET-Example


Cookie.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="15_cookies.aspx.cs" Inherits="_15_cookies" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 100%;
        }
        .auto-style2 {
            width: 132px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <table class="auto-style1">
            <tr>
                <td class="auto-style2">
                    <asp:Label ID="Label2" runat="server" Text="Name"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">
                    <asp:Label ID="Label1" runat="server" Text="No"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </td>
            </tr>
        </table>
   
    </div>
        <p>
            <asp:Button ID="Button1" runat="server" Height="30px" OnClick="Button1_Click" Text="OK" Width="100px" />
            <asp:Button ID="Button2" runat="server" Height="30px" Text="PRINT" Width="100px" OnClick="Button2_Click" />
        </P>
    </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;

public partial class _15_cookies : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Cookies["nm"].Value = TextBox1.Text;
        Response.Cookies["no"].Value = TextBox2.Text;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
       
       
        string name = string.Empty;
        string nom = string.Empty;
       
        name = Request.Cookies["abc"].Value;
        nom = Request.Cookies["abc"].Value;

        Response.Write("Name=>" + name);
        Response.Write("Number=>" + nom);
    }
}

Share this

Related Posts

First