In ASP.NET, cookies are small pieces of data that are stored on the client-side (i.e., in the user's web browser) and are sent between the client and the server with each HTTP request and response. Cookies are used to store user-specific information and preferences across multiple requests, enabling a stateful web experience in an otherwise stateless environment like the HTTP protocol.
protected void btncreate_Click(object sender, EventArgs e)
{
Response.Cookies["name"].Value = txtcreatecookie.Text;
Response.Cookies["name"].Expires = DateTime.Now.AddMinutes(1);
Label1.Text = "Cookie Created";
txtcreatecookie.Text = "";
}
protected void btnretrieve_Click(object sender, EventArgs e)
{
if (Request.Cookies["name"] == null)
{
txtretrieve.Text = "No cookie found";
}
else
{
txtretrieve.Text = Request.Cookies["name"].Value;
}
}
we create cookie with name parameter and assign textbox values to name cookie.
Set the expiration time to 1 minute, after which the cookie will be deleted, and a 'No cookie found' message will be displayed.