Monday, 15 July 2013

How to create cookies and Read Cookies in asp.net

How to create cookies and Read Cookies in asp.net



Description                                                                    Likes 0
Cookies are used by Web sites to keep track of their visitors.This website uses cookies to allow us to see how the site is used. in this example we will tell you how to create cookies and how to read it again.
Html Source Code :
01<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SetCookiesValues.aspx.cs"
02    Inherits="SetCookiesValues" %>
03 
04<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
05<html xmlns="http://www.w3.org/1999/xhtml">
06<head id="Head1" runat="server">
07    <title>how to Create cookies value and read cookies value in asp.net</title>
08    <link href="stylesheet.css" rel="stylesheet" type="text/css" />
09</head>
10<body>
11    <form id="form1" runat="server">
12    <div style="margin: 25%; font-family: verdana; padding: 5px 10px 5px 10px;" class="back-form">
13        <div style="margin-left: 12px">
14            <span style="color: #336699; font-size: larger;">Cookie Examples </span>
15            <br />
16            <br />
17            <table style="width: 100%;">
18                <tr>
19                    <td>
20                         </td>
21                    <td>
22                       <span style="font-size:x-small">Create Cookies and Read Cookies</span></td>
23                </tr>
24                <tr>
25                    <td>
26                        <asp:Label ID="Label1" runat="server">Email Id</asp:Label>
27                    </td>
28                    <td>
29                        <asp:TextBox ID="TextBox1" runat="server" CssClass="textbox-style"Width="250px"></asp:TextBox>
30                    </td>
31                </tr>
32                <tr>
33                    <td>
34                        <asp:Label ID="Label2" runat="server">Password</asp:Label>
35                    </td>
36                    <td>
37                        <asp:TextBox ID="TextBox2" runat="server" CssClass="textbox-style"
38                            Width="250px" TextMode="Password"></asp:TextBox>
39                    </td>
40                </tr>
41                <tr>
42                    <td>
43                        <br />
44                    </td>
45                    <td>
46                        <asp:Button ID="Button1" runat="server" CssClass="black-button"OnClick="Button1_Click"
47                            Text="Set Cookie " Width="120px" />
48                         <asp:Button ID="Button2" runat="server" CssClass="black-button"Text="Read Cookie"
49                            Width="120px" onclick="Button2_Click" />
50                    </td>
51                </tr>
52                <tr>
53                    <td colspan="2">
54                        <asp:Label ID="Label3" runat="server"></asp:Label>
55                    </td>
56                </tr>
57            </table>
58        </div>
59    </div>
60    </form>
61</body>
62</html>
C# Code Behind
01using System;
02using System.Collections;
03using System.Configuration;
04using System.Data;
05using System.Linq;
06using System.Web;
07using System.Web.Security;
08using System.Web.UI;
09using System.Web.UI.HtmlControls;
10using System.Web.UI.WebControls;
11using System.Web.UI.WebControls.WebParts;
12using System.Xml.Linq;
13 
14 
15public partial class SetCookiesValues : System.Web.UI.Page
16{
17    protected void Page_Load(object sender, EventArgs e)
18    {
19 
20        //Delete Cookies if already exist....
21        Response.Cookies["LoginInfoCookies"].Expires = DateTime.Now.AddDays(-1);
22 
23 
24    }
25    protected void Button1_Click(object sender, EventArgs e)
26    {
27        HttpCookie LoginInfoCookies = new HttpCookie("LoginInfoCookies");
28        LoginInfoCookies["Email Id"] = TextBox1.Text.ToString();
29        LoginInfoCookies["Password"] = TextBox2.Text.ToString();
30        Response.Cookies.Add(LoginInfoCookies);
31        // show message
32        Label3.ForeColor = System.Drawing.Color.Green;
33        Label3.Font.Size = FontUnit.Small;
34        Label3.Text = "Cookies is Saved Successfully !";
35    }
36    protected void Button2_Click(object sender, EventArgs e)
37    {
38        HttpCookie LoginInfoCookies = Request.Cookies["LoginInfoCookies"];
39        if (LoginInfoCookies != null)
40        {
41            string Email = LoginInfoCookies["Email Id"];
42            string pass = LoginInfoCookies["Password"];
43            // show message
44             
45            Label3.Font.Size = FontUnit.Small;          
46            Label3.ForeColor = System.Drawing.Color.Black;
47           Label3.Text = "Cookies Successfully Read <br>" +
48                         " Email Id   : " + Email + "<br> Password : " + pass;
49 
50        }
51        
52    }
53}

No comments:

Post a Comment