I wrote a demo about HTTP cookie today and found that the cookie value will be lost as long as vs is re-run. The Code is as follows:
Protected void page_load (Object sender, eventargs e) {httpcookie _ userinfocookies = request. Cookies ["username"]; string city; If (_ userinfocookies! = NULL) {city = _ userinfocookies ["city"]; session ["location"] = city; response. redirect ("berava. aspx ") ;}} protected void moveto_click (Object sender, eventargs e) {session [" location "] = worlddrdolist. selecteditem. value; If (checkbox1.checked) {httpcookie _ userinfocookies = new httpcookie ("username"); _ userinfocookies ["city"] = worlddrdolist. selectedvalue. tostring (); response. cookies. add (_ userinfocookies);} response. redirect ("berava. aspx ");}
The last result is because the cookie expiration time is not set (expire). The default cookie is only valid for the current session, so vs is a new session when it is re-run, in this case, the cookie has expired. You only need to set an expiration time for the cookie, as shown below (the effective time is one day ):
_ Userinfocookies. expires = datetime. Now. adddays (1 );