Tuesday, May 1, 2012

Working with Cookies in ASP.net


   Cookies are the part of a state management in asp.net to store server information at client side. It has two types
      a. In-Memory Cookies
b. Persistent Cookies

 In-Memory Cookies
     cookies information stored as long as the current application is opened.
  Persistent Cookies
    cookies information stored as long as the current application is opened and leave the the application (both online and offline).It is stored temporarily based on date and time.

         Page Design (Source code) :-
     
             <%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="FrmInMemoryCookies1.aspx.cs" Inherits="StateManagement_FrmInMemoryCookies1" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
    </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
    <div align="center">
   <fieldset style="width:300px;height:200px">
 
       <table class="style1">
           <tr>
               <td>
                   &nbsp;</td>
               <td>
                   &nbsp;</td>
           </tr>
           <tr>
               <td>
                   &nbsp;</td>
               <td>
                   &nbsp;</td>
           </tr>
           <tr>
               <td style="text-align: right" width="30%">
                   <asp:Label ID="Label1" runat="server" Text="Enter Name"></asp:Label>
               </td>
               <td style="text-align: left" width="70%">
                   <asp:TextBox ID="TxtName" runat="server"></asp:TextBox>
               </td>
           </tr>
           <tr>
               <td>
                   &nbsp;</td>
               <td>
                   <asp:Button ID="BtnSubmit" runat="server" onclick="BtnSubmit_Click"
                       Text="In-MemoryCookies" />
                        <asp:Button ID="BtnPcookies" runat="server" Text="PersistentCookies"
                       onclick="BtnPcookies_Click" />
                   <asp:Button ID="BtnRefresh" runat="server" onclick="BtnRefresh_Click"
                       Text="ReStore" />
               </td>
           </tr>
           <tr>
               <td>
                   &nbsp;</td>
               <td>
                  <asp:Label ID="Label2" runat="server" Text=""></asp:Label>
               </td>
           </tr>
         
       </table>
 
 </fieldset>
</div>

</asp:Content>



C# Code :-



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class StateManagement_FrmInMemoryCookies1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
     
            if (Request.Cookies["name"] == null)
            {
            }
            else
            {
                TxtName.Text = Request.Cookies["name"].Value;
            }
     
    }
    protected void BtnSubmit_Click(object sender, EventArgs e)
    {    

            HttpCookie x = new HttpCookie("name");
            x.Value = TxtName.Text;
            Response.Cookies.Add(x);
            Label2.Text = "Information Stored in Cookies";
            TxtName.Text = "";
     
    }
    protected void BtnRefresh_Click(object sender, EventArgs e)
    {
        Label2.Text = "Cookies Information ReStored";
    }
    protected void BtnPcookies_Click(object sender, EventArgs e)
    {
        HttpCookie x = new HttpCookie("name");
        x.Value = TxtName.Text;
        x.Expires = DateTime.Now.AddMinutes(5);
        Response.Cookies.Add(x);
        Label2.Text = "Information Stored in Cookies";
        TxtName.Text = "";
    }
}













No comments:

Post a Comment