Thursday 9 February 2012

HOW TO CREATE COOKIES IN ASP.NET



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
public partial class _Default : System.Web.UI.Page
    {
    protected void bttLogin_Click(object sender, EventArgs e)
    {
    HttpCookie myCookie = new HttpCookie("yourInfo");
    myCookie.Values["yourName"] = txtUsername.Text;
    myCookie.Values["yourID"] = txtPassword.Text;
    Response.Cookies.Add(myCookie);
     }
    protected void bttGet_Click(object sender, EventArgs e)
    {
    HttpCookie getCookie;
    getCookie = Request.Cookies["yourInfo"];
    if (getCookie.HasKeys)
    {
    NameValueCollection valueCookie = new NameValueCollection(getCookie.Values);
    foreach (string k in valueCookie.AllKeys)
    {
    Response.Write(k + " : ");
    Response.Write(getCookie.Values[k] + "<br />");
     }
     }  
    else
     { Response.Write(getCookie.Value); }
    lblSum.Text = "Welcome .. " + getCookie.Values["yourName"];
     }
     }



No comments:

Post a Comment