Question : I need an explanation of this working aspx.cs cookie code...

My questions are:

1) Are the Request and Response objects automatically created?
2) One you add a cookie to the Response object, does it come back in the Request object?

I guess I just need a little more explanation of this..
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
using System;
using System.Data;
using System.Web;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Cookies["BackgroundColor"] != null)
        {
            if (!Page.IsPostBack)
            {
                ColorSelector.SelectedValue = Request.Cookies["BackgroundColor"].Value;
            }
            BodyTag.Style["background-color"] = ColorSelector.SelectedValue;
        }
    }

    protected void ColorSelector_IndexChanged(object sender, EventArgs e)
    {
        BodyTag.Style["background-color"] = ColorSelector.SelectedValue;
        HttpCookie cookie = new HttpCookie("BackgroundColor");
        cookie.Value = ColorSelector.SelectedValue;
        cookie.Expires = DateTime.Now.AddHours(0.02);
        Response.Cookies.Add(cookie);
    }
}

Answer : I need an explanation of this working aspx.cs cookie code...

1.) Yes, Request and Response objects are created automatically.
2.) Yes, the cookie travels with the Request object.
Random Solutions  
 
programming4us programming4us