View State plays an important role in the PostBack mechanism of ASP. NET, but the huge hiddenfield in a bandwidth-intensive environment (such as cross-country access) often becomes a system bottleneck. Is there any way to save the view State on the server? The answer is yes.
Method 1:
The Code projectArticleKeep ASP. NET viewstate out of ASPX page for performance improvement, with overridePageOfSavepagestatetopersistencemediumAndLoadpagestatefrompersistencemediumTwo Methods: store the view State in the session or cache.
However, this method is always too cumbersome. Is there any easier way?
Method 2:
There is such a section in ASP. NET 2.0 quick start.Page-State, (the cumulative view-state and control-state for the controls and the page) is typically persisted to the page's response, using Hiddenfieldstatepersister
(It is the viewstate hidden field). You can override Pagestatepersister
On Page
For custom persistence however. Given the adaptive nature of controls to the requesting device, some devices may not handle significant amounts of data that are typically 'round-tripped '. Through Pageadapter
Configured for a device, the state can be persisted to alternative sources. ASP. NET 2.0 defines two page state persisters, Hiddenfieldpagestatepersister
And Sessionpagestatepersister
.
In fact, ASP. NET 2.0 has built-in viewstate storage in the sessionSessionpagestatepersister, As long as the override Page'sPagestatepersisterAttribute to returnHiddenfieldpagestatepersisterChange to returnSessionpagestatepersisterYou can. This is really convenient.
But I found another unpleasant thing. Let's take a look at page. pagestatepersister's Code : Protected Virtual Pagestatepersister
{
Get
{
If ( This . _ Persister = Null )
{
Pageadapter adapter1 = This . Pageadapter;
If (Adapter1 ! = Null )
{
This . _ Persister = Adapter1.getstatepersister ();
}
If ( This . _ Persister = Null )
{
This . _ Persister = New Hiddenfieldpagestatepersister ( This );
}
}
Return This . _ Persister;
}
}
Although we can reloadPagestatepersisterAttribute, but cannot be modified_ Persister. InPageIs called directly._ Persister. This may cause a certain degree of inconsistency.
How can this problem be solved? NoticePageadapter? Let's take it.
Method 3:
First, write your ownPageadapter
Using System;
Using System. Collections. Generic;
Using System. text;
Using System. Web. UI;
Namespace My
{
Public Class Pagestateadapter: system. Web. UI. adapters. pageadapter
{
Public Override Pagestatepersister getstatepersister ()
{
Return New Sessionpagestatepersister ( This . Page );
}
}
}
Then add App_browsers Directory, and add Pagestateadapter. Browser : < Browsers >
< Browser RefID = "Default" >
< Controladapters >
< Adapter Controltype = "System. Web. UI. Page" Adaptertype = "My. pagestateadapter" />
</ Controladapters >
</ Browser >
</ Browsers >
Now our server side view State is complete. You can compare the size of the files generated before and after, and the more complex the page, the more obvious the effect. Of course, this is at the cost of server performance. Note that the so-called server side depends on how the session storage method of the site is configured, but this is not covered in this article.