Cookies保存了一些信息,转到另外一个页面后无法读取了,跟踪显示Cookies值为空了,请高手帮忙解决下,谢谢先了!!保存Cookies代码:
protected void btnSetCookiesClick(object sender, EventArgs e)
        {
            cookiesManagerTest1.AddSubkey(txtSetCookiesName.Text,txtSetCookiesValue.Text);
            Response.Redirect("CookiesManagerGet.aspx");
        }
读取Cookies代码:
protected void btnGetCookiesClick(object sender, EventArgs e)
        {
            HttpCookie cookies = Request.Cookies["ManagerTest"];
            if (cookies.HasKeys)
            {
                if (!String.IsNullOrEmpty(cookies.Values[txtCookiesName.Text]))
                {
                    txtGetCookies.Text = cookies.Values[txtCookiesName.Text];
                }
            }
        }其中cookiesManagerTest1是一个自定义用户控件的ID,
控件代码如下
public class CookiesManager : WebControl
    {
        #region Private
        private string _cookieName = "";
        private string _cookieSubkey = "";
        private string _cookieValue = "";
        private bool _dataBound;
        private DateTime _expireDate = DateTime.Now.AddDays(3650);
        private HttpCookie _cookie;
        #endregion
        #region Properties
        public string CookieValue
        {
            get { return GetCookieValue(); }
            set { _cookieValue = value; }
        }        public string CookieName
        {
            get { return _cookieName; }
            set { _cookieName = value; }
        }        public string CookieSubkey
        {
            get { return _cookieSubkey; }
            set { _cookieSubkey = value; }
        }        public DateTime ExpireDate
        {
            get { return _expireDate; }
            set { _expireDate = value; }
        }
        #endregion        #region Contructors
        public CookiesManager()
        {
        }        public CookiesManager(string cookiename, string cookiesubkey, string cookievalue, DateTime expiredate)
        {
            _cookieName = cookiename;
            _cookieSubkey = cookiesubkey;
            _cookieValue = cookievalue;
            _expireDate = expiredate;
        }
        #endregion        #region Cookie Control event
        /// <summary>
        /// Setup the CookiesManager instance in the pages controlstate control collection
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);            if (Page.Items[typeof(CookiesManager)] == null)
            {
                Page.Items[typeof(CookiesManager)] = this;
            }            Page.RegisterRequiresControlState(this);
        }        /// <summary>
        /// In the control load, if the Cookie doesn't exist, then will CreateCookie
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);            if (!_dataBound)
            {
                DataBind();
            }
            if (!CookieExisted())
                CreateCookie();
            else
                RenewCookie();
        }        public override void DataBind()
        {
            base.DataBind();
            _dataBound = true;
        }
        #endregion
        #region Public Methods
        public string GetCookieValue()
        {
            string tmpValue = "";
            if (CookieExisted())
            {
                if (string.IsNullOrEmpty(CookieSubkey))
                    tmpValue = HttpContext.Current.Server.HtmlEncode(HttpContext.Current.Request.Cookies[CookieName].Value);
                else
                    tmpValue = HttpContext.Current.Server.HtmlEncode(HttpContext.Current.Request.Cookies[CookieName][CookieSubkey]);
            }
            return tmpValue;
        }        public string GetCookieValue(string cookieSubkey)
        {
            string tmpValue = "";
            if (CookieExisted())
            {
                tmpValue = HttpContext.Current.Server.HtmlEncode(HttpContext.Current.Request.Cookies[CookieName][cookieSubkey]);
            }
            return tmpValue;
        }        /// <summary>
        /// this function remove a subkey, and will do nothing if the subkey doesn't exist
        /// </summary>
        /// <param name="subkeyname"></param>
        public void RemoveSubkey(string subkeyname)
        {
            if (CookieExisted())
            {
                _cookie = HttpContext.Current.Request.Cookies[CookieName];
                if (_cookie.Values[subkeyname] != null)
                    _cookie.Values.Remove(subkeyname);
            }
        }        /// <summary>
        /// this function adds a new subkey to the existing cookie, if the subkey already existed, 
        /// then the fucntion will update the value of the subkey
        /// </summary>
        /// <param name="subkeyname"></param>
        /// <param name="subkeyvalue"></param>
        public void AddSubkey(string subkeyname, string subkeyvalue)
        {
            if (CookieExisted())
            {
                _cookie = HttpContext.Current.Request.Cookies[CookieName];
                if (_cookie.Values[subkeyname] == null)
                    _cookie.Values.Add(subkeyname,subkeyvalue);
                else
                    _cookie.Values[subkeyname] = subkeyvalue;
                _cookie.Expires = ExpireDate;
            }
        }        /// <summary>
        /// this function set the cookie.Expires to the ExpireDate
        /// </summary>
        public void RenewCookie()
        {
            if (CookieExisted())
            {
                _cookie = HttpContext.Current.Request.Cookies[CookieName];
                _cookie.Expires = ExpireDate;
            }
        }        /// <summary>
        /// this function removes a cookie with the list of cookies with the specific CookieName
        /// </summary>
        public void DeleteCookie()
        {
            if (CookieExisted())
            {
                _cookie = HttpContext.Current.Request.Cookies[CookieName];
                _cookie.Expires = DateTime.Now.AddDays(-1);
                HttpContext.Current.Response.Cookies.Remove(CookieName);
            }
        }        #endregion
        #region Private Methods
        /// <summary>
        /// this function checks whether the Cookie Existed with the specified CookieName
        /// </summary>
        /// <returns></returns>
        private bool CookieExisted()
        {
            return ((HttpContext.Current.Request.Cookies[CookieName] != null) && (!string.IsNullOrEmpty(CookieName)));
        }        private void CreateCookie()
        {
            _cookie = new HttpCookie(CookieName);
            HttpContext.Current.Response.Cookies.Add(_cookie);
            if (string.IsNullOrEmpty(CookieSubkey))
                _cookie.Value = _cookieValue;
            else
                AddSubkey(CookieSubkey, _cookieValue);
            _cookie.Expires = ExpireDate;
            if (Core.IsTestMode)
            {
                HttpContext.Current.Response.Write("CookieName=" + CookieName + "<Br>");
                HttpContext.Current.Response.Write("CookieSubkey=" + CookieSubkey + "<Br>");
                HttpContext.Current.Response.Write("CookieValue=" + CookieValue + "<Br>");
                HttpContext.Current.Response.Write("CookieExisted=" + CookieExisted() + "<Br>");
            }
        }
        #endregion
    }