Dot Net Sparks C#.NET,ASP.NET,ADO.NET,LINQ,AJAX,JavaScript,SharePoint

/ 0

Cookies in ASP.NET

A cookie is a small bit of text that accompanies requests and pages as they go between the Web server and browser. The cookie contains information the Web application can read whenever the user visits the site.

This example shows how to work with cookies in asp.net.

Create a new website in VS and add the following code that designs our aspx page with two text boxes and three buttons.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" EnableEventValidation="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Cookies Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <b>Cookies Example</b>
        <table cellpadding="2" cellspacing="2">
            <tr>
                <td colspan="2">
                    <asp:Label ID="lblInfo" runat="server" Text="Label" ForeColor="#993300"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:TextBox ID="txtSetCookie" runat="server"></asp:TextBox>
                </td>
                <td>
                    <asp:Button ID="btnSetCookie" runat="server" Text="Set Cookie" OnClick="btnSetCookie_Click"
                        Width="120px" />
                </td>
            </tr>
            <tr>
                <td>
                    <asp:TextBox ID="txtGetCookie" runat="server" Enabled="False"></asp:TextBox>
                </td>
                <td>
                    <asp:Button ID="btnGetCookie" runat="server" Text="Get Cookie" OnClick="btnGetCookie_Click"
                        Width="120px" />
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;
                </td>
                <td>
                    <asp:Button ID="btnDeleteCookie" runat="server" Text="Delete Cookie" OnClick="btnDeleteCookie_Click"
                        Width="120px" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

And it’s code behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    HttpCookie cookie;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lblInfo.Visible = false;
        }
        else
        {
            lblInfo.Visible = true;
        }
      

    }
    protected void btnSetCookie_Click(object sender, EventArgs e)
    {
        //Checks wheather the text box is empty or not
        if (txtSetCookie.Text != null && txtSetCookie.Text != "")
        {
            cookie = new HttpCookie("MyCookie");
            cookie.Value = txtSetCookie.Text;
            cookie.Expires = DateTime.Now.AddMinutes(1);
            Response.SetCookie(cookie);
            lblInfo.Text = "Cookie created.";
            txtSetCookie.Text = "";
        }
        else
        {
            lblInfo.Text = "Please enter cookie value.";

        }

    }
    protected void btnGetCookie_Click(object sender, EventArgs e)
    {
        txtGetCookie.Text = "";
        //Checks wheather the cookie is null or not
        if (Request.Cookies["MyCookie"] != null)
        {
            txtGetCookie.Text = Request.Cookies["MyCookie"].Value;
        }
        else
        {
            lblInfo.Text = "Please set cookie.";
        }
    }
    protected void btnDeleteCookie_Click(object sender, EventArgs e)
    {
        if (Request.Cookies["MyCookie"] != null)
        {
            Response.Cookies["MyCookie"].Expires = DateTime.Now.AddDays(-1);
            lblInfo.Text = "Cookie deleted.";
            txtGetCookie.Text = "";
        }
        else
        {
            lblInfo.Text = "Please set cookie.";
        }
    }
}


0 comments
Comments (0)

Leave a comment