Front-end:
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: button id = "button1" runat = "server" text = "Write cookie value"
Onclick = "button#click"/>
<Br/>
<Asp: button id = "button2" runat = "server" text = "read cookie value"
Onclick = "button2_click"/>
<Asp: textbox id = "txtcookie" runat = "server"> </ASP: textbox>
<Br/>
</Div>
</Form>
</Body>
Background:
// Write cookie
Protected void button#click (Object sender, eventargs E)
{
// Define the content of a new cookie
// Instantiate a cookie object. "name" is the name of the cookie (the name is written to the client)
Httpcookie cookie = new httpcookie ("name ");
// Write the name content
Cookie. value = "Lili ";
// Expires indicates the expiration time = datetime. Now. adddays (1) indicates that its expiration date is the current date plus one day.
Cookie. expires = datetime. Now. adddays (1 );
// This code is actually written to the client.
Response. Cookies. Add (cookie );
}
// Read cookie
Protected void button2_click (Object sender, eventargs E)
{
// Determine whether the text box is null
If (request. Cookies ["name"]! = NULL)
{
// If it is not empty, the name value in the cookie can be read.
Txtcookie. Text = request. Cookies ["name"]. value;
}
Else
{
Txtcookie. Text = "dear, you have not entered ";
}
}