In ASP. NET, viewstate occupies a large amount of space in the HTML of the client, and
PostBack is repeatedly transmitted to the network and has been suffering from human diseases. However, viewstate can be used to store data.
Library, cache, and other places, so as to avoid frequent sending lengthy base64 strings to the client. Do not
But it can significantly improve the performance (greatly reducing the number of bytes transmitted over the network), and if the content does not
It is easily decrypted and cracked. Therefore, this method is very useful.
The following is a simple example where cache is used as the storage destination of viewstate. As for the cached key
It is just a simple way of writing. You can provide a rigorous solution based on the situation.
CodeAn example is as follows:
<% @ Page Language = "C #" codebehind = "saveviewstatpolicther. aspx. cs"
Autoeventwireup = "false" inherits = "your list. saveviewstatpolicther" %>
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> saveviewstatpolicther </title>
<Meta name = "generator" content = "Microsoft Visual Studio. NET 7.1">
<Meta name = "code_language" content = "C #">
<Meta name = vs_defaultclientscript content = "JavaScript">
<Meta name = vs_targetschema
Content = "http://schemas.microsoft.com/intellisense/ie5">
</Head>
<Body ms_positioning = "gridlayout">
<Form ID = "form1" method = "Post" runat = "server"> <asp: DataGrid id = datagrid1
Style = "Z-INDEX: 101; left: pixel; position: absolute; top: 72px"
Run at = "server" bordercolor = "# 3104cc" borderstyle = "none" borborwidth = "1px"
Backcolor = "white" cellpadding = "4" pagesize = "6" allowpaging = "true">
<Selecteditemstyle font-bold = "true" forecolor = "# ccff99" backcolor = "#009999">
</Selecteditemstyle>
<Itemstyle forecolor = "#003399" backcolor = "white">
</Itemstyle>
<Headerstyle font-bold = "true" forecolor = "# ccccff" backcolor = "#003399">
</Headerstyle>
<Footerstyle forecolor = "#003399" backcolor = "#99 CCCC">
</Footerstyle>
<Pagerstyle horizontalalign = "Left" forecolor = "#003399" backcolor = "#99 CCCC"
Pagebuttoncount = "20" mode = "numericpages">
</Pagerstyle>
</ASP: DataGrid>
</Form>
</Body>
</Html>
Using system;
Using system. Data;
Using system. IO;
Using system. text;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Namespace shortlist
{
/// <Summary>
/// Summary of saveviewstatpolicther.
/// </Summary>
Public class saveviewstatpolicther: Page
{
Protected DataGrid datagrid1;
Private void page_load (Object sender, eventargs E)
{
If (! Ispostback)
BIND ();
}
private void BIND ()
{< br> datatable table = new datatable ();
table. columns. add ("ID", typeof (INT);
table. columns. add ("name", typeof (string);
for (INT I = 0; I <1000; I ++)
{< br> datarow ROW = table. newrow ();
row ["ID"] = I;
row ["name"] = "Student _" + I. tostring ();
table. rows. add (ROW);
}< br> datagrid1.datasource = table;
datagrid1.databind ();
}
# Code generated by region web Form Designer
Protected override void oninit (eventargs E)
{
Initializecomponent ();
Base. oninit (E );
}
Private void initializecomponent ()
{
This. Maid + = new
System. Web. UI. webcontrols. datagridpagechangedeventhandler
(This. Maid pageindexchanged );
This. Load + = new system. eventhandler (this. page_load );
}
# Endregion
Protected override void savepagestatetopersistencemedium (Object
Viewstate)
{
Losformatter format = new losformatter ();
Stringwriter writer = new stringwriter ();
Format. serialize (writer, viewstate );
String vsraw = writer. tostring ();
Byte [] buffer = convert. frombase64string (vsraw );
String vstext = encoding. ASCII. getstring (buffer );
Object v = cache [pagekey];
If (V = NULL)
Cache. insert (pagekey, vstext );
Else
Cache [pagekey] = vstext;
}
Public String pagekey
{
Get {return session. sessionid +
"_ Page_saveviewstat1_ther_aspx ";}
}
Protected override object loadpagestatefrompersistencemedium ()
{
Object S = cache [pagekey];
If (s! = NULL)
{
String state = S. tostring ();
Byte [] buffer = encoding. ASCII. getbytes (State );
String vsraw = convert. tobase64string (buffer );
Losformatter formatter = new losformatter ();
Return formatter. deserialize (vsraw );
}
Return NULL;
}
Private void datagrid#pageindexchanged (Object source,
Datagridpagechangedeventargs E)
{< br> datagrid1.currentpageindex = E. newpageindex;
BIND ();
}< BR >}< br> for actual applications, if you want to apply this scheme to the entire Program , a general page base class is used.