Save and retrieve values in Cookie (C#) : Cookie « Session Cookie « ASP.Net






Save and retrieve values in Cookie (C#)

<%@ Page Language="c#" %>
<script Language="c#" runat="server">
  void Page_Load(object source, EventArgs e)
  {
    if (!(IsPostBack))
    {
      MyButton.Text = "Save Cookie";
      MyDropDownList.Items.Add("Blue");
      MyDropDownList.Items.Add("Red");
      MyDropDownList.Items.Add("Gray");
    }
  }
  public void Click(object sender, EventArgs e)
  {
    HttpCookie MyCookie = new HttpCookie("Background");
    MyCookie.Value = MyDropDownList.SelectedItem.Text;
    Response.Cookies.Add(MyCookie);
  }

</script>

<html>
  <body>
    <form id="CookieForm" method="post" runat="server">
      <asp:DropDownList id=MyDropDownList runat="server"/>
      <asp:button id=MyButton runat="server" OnClick="Click"/>
    </form>
  </body>
</html>


///////////////////////////////////////////////////


<%@ Page Language="c#" %>
  <script Language="c#" runat="server">
    void Page_Load(object source, EventArgs e)
    {
      Response.Cache.SetExpires(DateTime.Now);
    }
    string GetBackground()
    {
      return Request.Cookies["Background"].Value;
    }
  </script>
  <html>
    <body bgcolor="<% Response.Write(GetBackground()); %>">
    <asp:label id="message" runat="server" />
    </body>
  </html>

           
       








Related examples in the same category

1.Create and retrieve Cookie data (C#)
2.Display all Cookie (C#)
3.Delete Cookie (C#)
4.Delete All Cookies (C#)
5.Setting a Cookie (VB.net)
6.Adding cache dependencies (VB.net)
7.Getting cookie values in ASP.NET
8.Setting expire date and path for cookies in ASP.NET (VHB.net)
9.Write Cookie and read Cookie (VB.net)
10.Save Cookie (Vb.net)
11.Set cookie expire date (VB.net)