Write and read cookie values

Source: Internet
Author: User

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 ";
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.