现在的位置: 首页 > 综合 > 正文

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

2012年11月06日 ⁄ 综合 ⁄ 共 4173字 ⁄ 字号 评论关闭

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());
    
    }
第一种方法:
         第一步,在Web.config文件的Pages配置节点中设置enableViewState="false",或者在每个Aspx页头部设置。完成第一步后,无论如何.Net也会在页面上输出<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="" />这样的内容。
         第二步,将<head runat="server">和<form id="form1" runat="server">中的runat="server"属性删除。完成第二步之后,asp.net默认不会再往客户端输出__VIEWSTATE元素。

 

抱歉!评论已关闭.