ASP. NET 2.0 Page Status persistence Program
Developers of ASP. NET controls use ViewState and control state to maintain the state information between various requests sent by the browser. Generally, this information is transmitted to the client as a hidden field in the HTML Tag displayed on the page. Then, the page status is returned to the server as part of the next form submission and restored to the control or page. Even if the browser uses the HTTP protocol, this protocol is defined as stateless), but with the function of temporary storage status information, control developers can easily provide richer application experience.
ASP. NET 2.0 allows you to modify the location and method of temporarily retaining the page status. In some cases, it is more desirable to avoid sending data back and forth between the client and the server. ASP. NET 2.0 provides two page state persistence programs: HiddenFieldPageState, which we have mentioned) and SessionPageStatePersister. SessionPageStatePersister uses Server sessions related to browser sessions to store data. SessionPageStatePersister has two positive and negative aspects. For pages sent to the browser, use sessions instead of hidden fields) to avoid increasing the size. In many cases, page status is an important part of all tags. However, storing data in sessions occupies valuable server resources. In addition, hidden fields do not have timeout as the session does. You can configure an application to maintain the session to the backend database and avoid directly adding the load to the Web server. This will also expand to the Web field solution.
To use a continuous program other than the default persistence program, you must rewrite the PageStatePersister attribute of the page and return an instance of another persistence program. First, the following simple page fills an ArrayList with a large number of numbers and binds it to a GridView control.
- < !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- < script runat="server">
- protected override PageStatePersister PageStatePersister {
- get {
- return new SessionPageStatePersister(this);
- }
- }
- protected override void OnLoad(EventArgs e) {
- base.OnLoad(e);
- if (!IsPostBack) {
- ArrayList list = new ArrayList();
- for (int i = 0; i < 1000; i++)
- {
- list.Add(Convert.ToString(i));
- }
- GridView1.DataSource = list;
- GridView1.DataBind();
- }
- }
- < /script>
- < html >
- < head id="Head1" runat="server">
- < title>Untitled Page< /title>
- < /head>
- < body>
- < form id="form1" runat="server">
- < div>
- < asp:GridView ID="GridView1" runat="server" />
- < asp:Button ID="Button1" runat="server" Text="Submit" />< /div>
- < /form>
- < /body>
- < /html>
When you view the HTML displayed on the page, you will see a large hidden field used to transmit ViewState.
- < !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- < html >
- < head>< title>
- Untitled Page
- < /title>< /head>
- < body>
- < form name="form1" method="post" action="default2.aspx" id="form1">
- < div>
- < input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"
- value="/wEPDwUKMTQ0MDQzNjk2Ng9kFgICBA9kFgICAQ88KwANAgAPFgYeC18hRGF0YUJv
- dW5kZx4JUGFnZUNvdW50AgEeC18hSXRlbUNvdW50AhRkDBQrAAEWBh4EVHlwZRkrAh4ETmF
- tZQUESXRlbR4JRGF0YUZpZWxkBQEhFgJmD2QWKgIBD2QWAmYPDxYCHgRUZXh0BQEwZGQCAg
- 9kFgJmDw8WAh8GBQExZGQCAw9kFgJmDw8WAh8GBQEyZGQCBA9kFgJmDw8WAh8GBQEzZGQCB
- Q9kFgJmDw8WAh8GBQE0ZGQCBg9kFgJmDw8WAh8GBQE1ZGQCBw9kFgJmDw8WAh8GBQE2ZGQC
- CA9kFgJmDw8WAh8GBQE3ZGQCCQ9kFgJmDw8WAh8GBQE4ZGQCCg9kFgJmDw8WAh8GBQE5ZGQ
- CCw9kFgJmDw8WAh8GBQIxMGRkAgwPZBYCZg8PFgIfBgUCMTFkZAIND2QWAmYPDxYCHwYFAj
- EyZGQCDg9kFgJmDw8WAh8GBQIxM2RkAg8PZBYCZg8PFgIfBgUCMTRkZAIQD2QWAmYPDxYCH
- wYFAjE1ZGQCEQ9kFgJmDw8WAh8GBQIxNmRkAhIPZBYCZg8PFgIfBgUCMTdkZAITD2QWAmYP
- DxYCHwYFAjE4ZGQCFA9kFgJmDw8WAh8GBQIxOWRkAhUPDxYCHgdWaXNpYmxlaGRkGAEFCUd
- yaWRWaWV3MQ9nZMhHZ3iQZp62S8IR8fTJ5ZL42ira" />
- < /div>
- ...
When we add and override the PageStatePersister attribute and use the built-in SessionPageStatePersister, the behavior of the page remains unchanged, but the storage for a large amount of State data will change from hidden fields to session state.
- protected override PageStatePersister PageStatePersister
- {
- get
- {
- return new SessionPageStatePersister(this);
- }
- }
Note that in the source code of the page, the hidden field value is much smaller, but it does not completely disappear. ASP. NET will still transmit some of the smallest datasets in the page output.
- < input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"
- value="/wEPaA8FDzhjNzkyNTMzNjE1YWEyNxgBBQlHcmlkVmlldzEPZ2QZw
- 44JLJFcglwRl9TiNliE82yAuQ==" />
Other solutions for ASP. NET 2.0 Page Status persistence
In some scenarios, you may only want to add similar code to a small page set. Therefore, it is acceptable to add a simple rewrite like this. When you want a complete application or a large page set to own the row, you need a more centralized way to control it. There are several ways to achieve this. We can move the code for creating this program to the class inherited from the page:
- using System;
- using System.Data;
- using System.Configuration;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- public class PagePersisterBasePage : Page
- {
- public PagePersisterBasePage() {
- }
- protected override PageStatePersister PageStatePersister {
- get {
- return new SessionPageStatePersister(this);
- }
- }
- }
ASP. NET 2.0 allows you to use the "Inherits" Page command to specify the base type of the page. Then, ASP. NET inherits the code generated for the page from the base page, and does not need to copy the code in each page.
- < %@ Page Language="C#" Inherits="PagePersisterBasePage" %>
In addition, the configuration option enables us to set the page location so that all pages use a single base page type. On the web. config page, we have set pageBaseType and do not need to add the Inherits attribute to any page to obtain custom PageStatePersister behavior.
- < ?xml version="1.0"?>
- < configuration>
- < system.web>
- < pages pageBaseType="PagePersisterBasePage" />
- < /system.web>
- < /configuration>
Modifying PageStatePersister is not an easy task. Consider your application and deployment carefully. Although there is a related overhead in the hidden field to transfer ViewState back and forth, to keep the state there, there is a need for direct server resource consumption. As shown in the preceding example, you can insert a custom persistence program to store the status elsewhere, such as a backend database or a status service shared by the Web farm. In addition, as we have demonstrated, You Can centrally control the behavior of applications, or control by page on the page.
The above is the implementation principle of ASP. NET 2.0 page state persistence program.
- Custom set of ASP. NET 2.0 data binding controls
- Example of a list control: HeadlineList
- ASP. NET 2.0 Data Binding Mechanism: Generate controls
- Brief Introduction to the Development of ASP. NET 2.0 Data Binding
- Overview ASP. NET Excel Process calling