View state persistence mechanism for ASP.net 2.0

Source: Internet
Author: User
Tags abstract base64

Objective

As long as you have a little understanding of viewstate, you will know that the ASP.net page viewstate is generally stored in a hidden field of the page:

<input type= "hidden" name= "__viewstate" id= "__viewstate" value= "a Mess of Things" >

When we look at the source of the page, we see a lot of clutter (especially when the page has a DataGrid with a lot of data, or a GridView in the asp.net2.0), that's viewstate.

Basic knowledge

Because of the new changes in the ViewState persistence storage mechanism in asp.net2.0, it's a simple introduction to the relevant things.

In asp.net1.1, only the persistence mechanism of the page hidden field is provided, so that in some cases you have to give up the use of ViewState, imagine if you have tens of thousands of records in your DataGrid (don't think that this kind of abnormal need is not, someone has met), If ViewState is enabled, do you have the assurance that your IIS servers can withstand the network's recipients? Of course you can change your storage mechanism by rewriting the Page.savepagestatetopersistencemedium () method, but don't forget to rewrite Page.loadpagestatefrompersistencemedium (), They're a couple.

The default view state persistence mechanism in asp.net2.0 remains the state information as a BASE64 encoded string in a hidden HTML element on the page (an element with a Type property set to "hidden"). The ASP.net page performs this work using a Hiddenfieldpagestatepersister object and serializes and deserializes the object state information using a Istateformatter instance. Alternatively, for mobile clients with limited bandwidth and resources, you can also use the SessionPageStatePersister class to store the view state of the page in the session object on the server, but there is also a session persistence mechanism. Let's keep the page state in session, not in the page, which is a savings on bandwidth.

But if you want to delve into the ViewState persistence mechanism, the abstract class pagestatepersister you should know, to preserve view state on clients that cannot support existing view state persistence mechanisms, you can extend the PageStatePersister class, Introduce your own view state persistence method, and you can use the page adapter to configure the ASP.net application to use a different view state persistence mechanism depending on the type of client for which the page is provided. Classes derived from the PageStatePersister class must override the Save abstract method to store view state and control state in persistent media, while overriding the Load method to extract state information. If you need to serialize view state and control state to a string, you can use the Istateformatter object that is accessed through the Stateformatter property. It can efficiently serialize and deserialize object state information into BASE64 encoded strings. You can also rewrite the Stateformatter property to provide your own object state serialization mechanism, as described in my code, simple enough to see.

Persistence mechanism of ViewState

Hidden fields

This is not introduced, the default is this. As in the preface.

Session

In asp.net2.0, just rewrite the PageStatePersister property.

protected override PageStatePersister PageStatePersister
{
  get
  {
return new SessionPageStatePersister(Page);
  }
}

If you need to rewrite the loadpagestatefrompersistencemedium in asp.net1.1, the two methods are:

protected override object LoadPageStateFromPersistenceMedium ()
{
  return Session["ViewState"];
}
protected override void SavePageStateToPersistenceMedium(object viewState)
{
  Session["ViewState"] = viewState;
  RegisterHiddenField("__VIEWSTATE", "");
}

Related 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.