using System; using System.Collections.Generic; using System.Web; namespace SZXJ.Common { public class PageState { static HttpRequest Request { get { return HttpContext.Current.Request; } } static HttpResponse Response { get { return HttpContext.Current.Response; } } const string PAGE_STATE_KEY = "page_state2"; Dictionary<string, string> m_state = new Dictionary<string, string>(); void Load() { m_state.Clear(); HttpCookie cookie = Request.Cookies[PAGE_STATE_KEY]; if (cookie == null) return; ArcByteGet aget = new ArcByteGet(cookie.Value); int count = aget.GetInt(0); for(int i = 0; i < count; i++) { string key = aget.GetString(string.Empty); string val = aget.GetString(string.Empty); if (!string.IsNullOrEmpty(key)) m_state[key] = val; } } void Save() { ArcByteSet aset = new ArcByteSet(); aset.Set(m_state.Count); foreach(KeyValuePair<string, string> pos in m_state) { aset.Set(pos.Key); aset.Set(pos.Value); } Response.Cookies.Remove(PAGE_STATE_KEY); HttpCookie cookie = new HttpCookie(PAGE_STATE_KEY, aset.ToBase64()); cookie.Expires = DateTime.MinValue; cookie.Path = Request.Path; Response.Cookies.Add(cookie); } public string this[string key] { get { if (m_state.Count == 0) Load(); return m_state.ContainsKey(key) ? m_state[key] : string.Empty; } set { m_state[key] = value; Save(); } } } }