Please refer the below code and see how i am creating and deleting the cookies
<form id="form1" runat="server">
<div>
<asp:Button ID="btnGetValues" runat="server" Text="Get Values from Cookie" OnClick="GetCookie" />
<br />
Value from Cookie
<br />
Name
<br />
<asp:Label ID="lblName" runat="server" />
<br />
City
<br />
<asp:Label ID="lblCity" runat="server" />
<br />
<asp:Button ID="btnDeleteCookie" runat="server" Text="Delete Cookie" OnClick="DeleteCookie" />
</div>
</form>
C#:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
Response.Cookies["StudentCookies"]["Name"] = "Azim";
Response.Cookies["StudentCookies"]["City"] = "Mumbai";
Response.Cookies["StudentCookies"].Expires = DateTime.Now.AddDays(1);
}
}
protected void GetCookie(object sender, EventArgs e)
{
if (Request.Cookies["StudentCookies"] != null)
{
this.lblName.Text = Request.Cookies["StudentCookies"]["Name"];
this.lblCity.Text = Request.Cookies["StudentCookies"]["City"];
}
}
protected void DeleteCookie(object sender, EventArgs e)
{
if (Request.Cookies["StudentCookies"] != null)
{
Response.Cookies["StudentCookies"].Expires = DateTime.Now.AddDays(-1d);
this.lblName.Text = string.Empty;
this.lblCity.Text = string.Empty;
}
}
Thank You.