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: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49:
defaults.aspx ============= <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server"> <title>Cookies</title> </head> <body runat="server" id="BodyTag"> <form id="form2" runat="server"> <asp:DropDownList runat="server" id="ColorSelector" autopostback="true" onselectedindexchanged="ColorSelector_IndexChanged"> <asp:ListItem value="White" selected="True">Select color...</asp:ListItem> <asp:ListItem value="Red">Red</asp:ListItem> <asp:ListItem value="Green">Green</asp:ListItem> <asp:ListItem value="Blue">Blue</asp:ListItem> </asp:DropDownList> </form> </body> </html> defaults.aspx.cs ================ 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); } }