Thursday, 18 October 2012

cookies in asp.net with c#



using System.Collections.Specialized;


     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 keyTemp in valueCookie.AllKeys)
            {
                Response.Write(keyTemp + " : ");
                Response.Write(getCookie.Values[keyTemp] + "<br />");
            }
        }
        else
        { Response.Write(getCookie.Value); }
        lblSum.Text = "Welcome .. " + getCookie.Values["yourName"];
    }

No comments:

Post a Comment