• 去掉ASP。NET页面中的VIEWSTATE非存硬盘方法


    //作者:king2003 QQ:41347614
    using System;
    using System.IO;
    using System.Text;
    using System.Web;
    using System.Web.UI;

    public class clViewStateMng
    {
        private LosFormatter _formatter = null;
        private byte _VIEWSTATE_NUM_PAGES;
        private enumSaveType _VIEWSTATE_SAVE_TYPE;
        private HttpContext ohttpcontext = null;
        private static clViewStateMng ovs = null;
        public string VIEWSTATE_FIELD_NAME;
        private const string VIEWSTATE_PAGE_COUNT = "_lngVIEWSTATE";
        private const string VIEWSTATE_SESSION_OBJ = "_objVIEWSTATE";
        private string[] ViewStates;

        private string GetViewState(long lreqNo)
        {
            if (this._VIEWSTATE_NUM_PAGES < 1)
            {
                return null;
            }
            short index = (short)(lreqNo % ((long)this._VIEWSTATE_NUM_PAGES));
            if (this.ohttpcontext.Session["_objVIEWSTATE"] == null)
            {
                return null;
            }
            this.ViewStates = (string[])this.ohttpcontext.Session["_objVIEWSTATE"];
            string str = null;
            string str2 = lreqNo.ToString();
            if (str2 == this.ViewStates[index].Substring(0, str2.Length))
            {
                str = this.ViewStates[index].Substring(str2.Length);
            }
            return str;
        }

        public static clViewStateMng GetViewStateMng()
        {
            if (ovs == null)
            {
                ovs = new clViewStateMng();
                ovs._VIEWSTATE_SAVE_TYPE = enumSaveType.Session;
                ovs.VIEWSTATE_FIELD_NAME = "__VIEWSTATE_KEY";
                ovs._VIEWSTATE_NUM_PAGES = Convert.ToByte(5);
            }
            ovs.ohttpcontext = HttpContext.Current;
            return ovs;
        }

        public object LoadViewState()
        {
            string input = null;
            if (this._formatter == null)
            {
                this._formatter = new LosFormatter();
            }
            input = this.GetViewState(this.RequestNumber);

            if (input != null)
            {
                return this._formatter.Deserialize(input);
            }
            return null;
        }

        public void SaveViewState(object viewState, ref long reqNumber)
        {

            if (this._formatter == null)
            {
                this._formatter = new LosFormatter();
            }
            StringBuilder sb = new StringBuilder();
            StringWriter output = new StringWriter(sb);
            this._formatter.Serialize(output, viewState);
            reqNumber = this.SetViewState(sb.ToString());

        }

        private long SetViewState(string szViewState)
        {
            if (this._VIEWSTATE_NUM_PAGES < 1)
            {
                return 0L;
            }
            this.ohttpcontext.Session["_lngVIEWSTATE"] = (this.ohttpcontext.Session["_lngVIEWSTATE"] == null) ? 0L : (((long)this.ohttpcontext.Session["_lngVIEWSTATE"]) + 1L);
            long num = (long)this.ohttpcontext.Session["_lngVIEWSTATE"];
            short index = (short)(num % ((long)this._VIEWSTATE_NUM_PAGES));
            if (this.ohttpcontext.Session["_objVIEWSTATE"] == null)
            {
                this.ViewStates = new string[this._VIEWSTATE_NUM_PAGES];
                this.ohttpcontext.Session["_objVIEWSTATE"] = this.ViewStates;
            }
            else
            {
                this.ViewStates = (string[])this.ohttpcontext.Session["_objVIEWSTATE"];
            }
            this.ViewStates[index] = num.ToString() + szViewState;
            return num;
        }

        private long RequestNumber
        {
            get
            {
                long num = 0L;

                if (this.ohttpcontext.Request.Form[this.VIEWSTATE_FIELD_NAME] != null)
                {
                    num = Convert.ToInt64(this.ohttpcontext.Request.Form[this.VIEWSTATE_FIELD_NAME]);
                }

                return num;
            }
        }

        public enumSaveType SaveType
        {
            get
            {
                return this._VIEWSTATE_SAVE_TYPE;
            }
        }

        public enum enumSaveType
        {
            Page,
            Session
        }
    }


    使用方法:
        protected override object LoadPageStateFromPersistenceMedium()
        {

                clViewStateMng viewStateMng = clViewStateMng.GetViewStateMng();
                return viewStateMng.LoadViewState();
        }

        protected override void SavePageStateToPersistenceMedium(object viewState)
        {

                clViewStateMng viewStateMng = clViewStateMng.GetViewStateMng();

                    long num=0;
                    viewStateMng.SaveViewState(viewState, ref num);
                    this.RegisterHiddenField(viewStateMng.VIEWSTATE_FIELD_NAME, num.ToString());
       
        }
  • 相关阅读:
    纯CSS打造可折叠树状菜单
    c++ Constructor FAQ 继续
    Java设计模式偷跑系列(六)Singleton模式的建模与实现
    优秀的产品经理是怎样炼成的?
    Pki原则
    屌丝男初中丰富的工作实践反击
    Android Material Design带UI变化
    unity多边形uv地图
    BZOJ 1208 HNOI2004 宠物收容所 平衡树/set
    [连载]Java程序设计(04)---任务驱动的方法:工资结算系统
  • 原文地址:https://www.cnblogs.com/yeye518/p/2231675.html
Copyright © 2020-2023  润新知