Server Side viewstate storage on the server side (ASP. NET 2.0)

Source: Internet
Author: User
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.

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.