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