Basic usage of cookies in ASP. NET and JS

Source: Internet
Author: User

Cookie writing in ASP. NET:

Protected void button#click (object sender, EventArgs e) // write cookie

{

HttpCookie cookie = new HttpCookie ("name"); // two names are different; 1: instantiate a cookie object (equivalent to key-value pair name is key-cookie is value)

Cookie. Value = "marry"; // 2: cookie Value

Cookie. expires = DateTime. now. addDays (1); // 3: cookie expiration time (add any number of expiration times per unit to the current time); if no expiration time is set, the cookie will not be written to the hard disk, this memory only exists in the memory. When the browser is closed, the cookie will be recycled. (temporary cookie)

Response. Cookies. Add (cookie); // 4: The object is put into the set.

}

Protected void Button2_Click (object sender, EventArgs e) // read cookie

{

If (Request. Cookies ["name"]! = Null) // read

{

TextBox1.Text = Request. Cookies ["name"]. Value; // Cookies ["name"] indicate that the cookie defined above is a key-value pair. Value.

}

Else

{

TextBox1.Text = "No! ";

}

}

Cookie writing in js:

Function writecookie (){

Var today = new Date ();

Today. setDate (today. getDate () + 1 );

Document. cookie = 'name = My songs; expires = '+ today. toGMTString (); // set the cookie value and cookie expiration time in the document

Document. cookie = 'username = Qu wanting; expires = '+ today. toGMTString ();

Document. cookie = 'password = 123; expires = '+ today. toGMTString ();

}

Function read () // read the value in the cookie

{

// Read multiple

Var cookie = document. cookie;

Var arry = cookie. split (';');

Alert (arry );

For (var I in arry) // different from c #

{

Var a = arry [I]. split ('= ');

// If (a [0] = "username") // if multiple cookies are read in a cookie file using this method, there is no space before the first name, spaces must be added before the names. Use trim to remove spaces.

//{

// Alert (a [1]);

//}

If (a [0]. trim ('') =" username ") // if multiple cookies in a cookie file are read using this method, there is no space before the first name, spaces must be added before the names in the future. Use trim to remove spaces (?????? Is there a problem? How can I say that trim is not supported? Firefox does not support ie and may not support configuration. What is the problem ?)

{

Alert (a [1]);

}

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.