C # Cookie Method

Source: Internet
Author: User

1 //Write2     protected voidButton1_Click (Objectsender, EventArgs e)3 {4HttpCookie cookie=NewHttpCookie ("Mycook");//Initial and set the name of the cookie5DateTime dt=DateTime.Now;6TimeSpan ts =NewTimeSpan (0,0,1,0,0);//expiration time is 1 minutes7Cookies. Expires = dt. ADD (TS);//Set Expiration Time8Cookies. Values.add ("userid","Userid_value");9Cookies. Values.add ("Userid2","userid2_value2");Ten Response.appendcookie (cookie); One       //output All content of this cookie A       //Response.Write (Cookies. Value);//output is: userid=userid_value&userid2=userid2_value2 - } -  the     //Read -     protected voidButton2_Click (Objectsender, EventArgs e) - { -  +       //HttpCookie Cokie = new HttpCookie ("Mycook");//the first order of the -       if(request.cookies["Mycook"]!=NULL) + { A         //Response.Write ("The value of the key in the cookie is userid:" + request.cookies["Mycook" ["userid"]);//entire Row at        //Response.Write ("Value of Userid2 in cookie" + request.cookies["Mycook" ["Userid2"]); -Response.Write (request.cookies["Mycook"]. Value);//Output All Values - } - } -  -     //Modify Cookies in     protected voidButton3_Click (Objectsender, EventArgs e) - { to       //Gets the cookie object for the client +HttpCookie Cok = request.cookies["Mycook"]; -          the       if(Cok! =NULL) * { $         //two ways to modify cookiesPanax NotoginsengCok. values["userid"] ="Alter-value"; -Cok. Values.set ("userid","Alter-value"); the  +         //Add new content to cookies ACok. Values.set ("newid","NewValue"); the Response.appendcookie (COK); + } - } $  $     //Delete Cookies -     protected voidButton4_Click (Objectsender, EventArgs e) - { the  -HttpCookie Cok = request.cookies["Mycook"];Wuyi       if(Cok! =NULL) the { -         if(!checkbox1.checked) Wu { -Cok. Values.remove ("userid");//remove a value with a key value of UserID About } $         Else - { -TimeSpan ts =NewTimeSpan (-1,0,0,0); -Cok. Expires = DATETIME.NOW.ADD (ts);//Delete the entire cookie, just set the expiration time to now A } + Response.appendcookie (COK); the } -}

C # Cookie Method

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.