Remove ASP. NET page viewstate non-storage Hard Disk Method

Source: Internet
Author: User

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
}
}

Usage:
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 ());

}
Method 1:
Step 1: Set enableviewstate = "false" in the pages configuration node of the web. config file, or set the header of each ASPX page. After completing step 1. net also outputs content such as <input type = "hidden" name = "_ viewstate" id = "_ viewstate" value = ""/> on the page.
Step 2: delete the runat = "server" attribute in

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.