|
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace InteractiveAspNet
{
/// <summary>
/// Summary description for AspNetCookies.
/// </summary>
public class AspNetCookies : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label myRequestCookie;
protected System.Web.UI.WebControls.Button btnSetCookie;
protected System.Web.UI.WebControls.Button btnClearCookie;
protected System.Web.UI.WebControls.Button btnDoNothing;
private void Page_Load(object sender, System.EventArgs e)
{
if (Request.Cookies["TestCookie"] != null)
Response.Cookies.Set(Request.Cookies["TestCookie"]);
else
Response.Cookies.Set(new HttpCookie("TestCookie", ""));
Response.Cookies["TestCookie"].Expires = DateTime.Now.AddYears(30);
// Display the Request cookie on the page
if (Response.Cookies["TestCookie"].Value == "")
myRequestCookie.Text = "Cookie is null";
else
myRequestCookie.Text = Request.Cookies["TestCookie"].Value;
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnSetCookie.Click += new System.EventHandler(this.btnSetCookie_Click);
this.btnClearCookie.Click += new System.EventHandler(this.btnClearCookie_Click);
this.btnDoNothing.Click += new System.EventHandler(this.btnDoNothing_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
protected override void OnPreRender(System.EventArgs e)
{
if (Response.Cookies["TestCookie"].Value == Request.Cookies["TestCookie"].Value)
Response.Cookies.Remove("TestCookie");
base.OnPreRender(e);
}
private void btnSetCookie_Click(object sender, System.EventArgs e)
{
// Set up a cookie and redirect to this page to pick it up for display
Response.Cookies["TestCookie"].Value = "Cookie is set";
Response.Redirect("AspNetCookies.aspx");
}
private void btnClearCookie_Click(object sender, System.EventArgs e)
{
// Expire the cookie and redirect to this page to display a message
Response.Cookies["TestCookie"].Expires = DateTime.Now.AddYears(-30);
Response.Redirect("AspNetCookies.aspx");
}
private void btnDoNothing_Click(object sender, System.EventArgs e)
{
// Do absolutely nothing except redirect to simulate moving to another page
Response.Redirect("AspNetCookies.aspx");
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.