ASP. NET 2.0 Page Status Persistence: ViewState and Control

Source: Internet
Author: User

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.

 
 
  1. < !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  3. < script runat="server"> 
  4.     protected override PageStatePersister PageStatePersister {  
  5.         get {  
  6.             return new SessionPageStatePersister(this);  
  7.         }  
  8.     }  
  9.     protected override void OnLoad(EventArgs e) {  
  10.         base.OnLoad(e);  
  11.         if (!IsPostBack) {  
  12.             ArrayList list = new ArrayList();  
  13.             for (int i = 0; i <  1000; i++)  
  14.             {  
  15.                 list.Add(Convert.ToString(i));  
  16.             }  
  17.             GridView1.DataSource = list;  
  18.             GridView1.DataBind();        
  19.         }  
  20.     }  
  21. < /script> 
  22. < html  > 
  23. < head id="Head1" runat="server"> 
  24.     < title>Untitled Page< /title> 
  25. < /head> 
  26. < body> 
  27.     < form id="form1" runat="server"> 
  28.     < div> 
  29.         < asp:GridView ID="GridView1" runat="server" />          
  30.         < asp:Button ID="Button1" runat="server" Text="Submit" />< /div> 
  31.     < /form> 
  32. < /body> 
  33. < /html> 

When you view the HTML displayed on the page, you will see a large hidden field used to transmit ViewState.

 
 
  1. < !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  3. < html  > 
  4. < head>< title> 
  5.    Untitled Page  
  6. < /title>< /head> 
  7. < body> 
  8.     < form name="form1" method="post" action="default2.aspx" id="form1"> 
  9. < div> 
  10. < input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"   
  11. value="/wEPDwUKMTQ0MDQzNjk2Ng9kFgICBA9kFgICAQ88KwANAgAPFgYeC18hRGF0YUJv  
  12. dW5kZx4JUGFnZUNvdW50AgEeC18hSXRlbUNvdW50AhRkDBQrAAEWBh4EVHlwZRkrAh4ETmF  
  13. tZQUESXRlbR4JRGF0YUZpZWxkBQEhFgJmD2QWKgIBD2QWAmYPDxYCHgRUZXh0BQEwZGQCAg  
  14. 9kFgJmDw8WAh8GBQExZGQCAw9kFgJmDw8WAh8GBQEyZGQCBA9kFgJmDw8WAh8GBQEzZGQCB  
  15. Q9kFgJmDw8WAh8GBQE0ZGQCBg9kFgJmDw8WAh8GBQE1ZGQCBw9kFgJmDw8WAh8GBQE2ZGQC  
  16. CA9kFgJmDw8WAh8GBQE3ZGQCCQ9kFgJmDw8WAh8GBQE4ZGQCCg9kFgJmDw8WAh8GBQE5ZGQ  
  17. CCw9kFgJmDw8WAh8GBQIxMGRkAgwPZBYCZg8PFgIfBgUCMTFkZAIND2QWAmYPDxYCHwYFAj  
  18. EyZGQCDg9kFgJmDw8WAh8GBQIxM2RkAg8PZBYCZg8PFgIfBgUCMTRkZAIQD2QWAmYPDxYCH  
  19. wYFAjE1ZGQCEQ9kFgJmDw8WAh8GBQIxNmRkAhIPZBYCZg8PFgIfBgUCMTdkZAITD2QWAmYP  
  20. DxYCHwYFAjE4ZGQCFA9kFgJmDw8WAh8GBQIxOWRkAhUPDxYCHgdWaXNpYmxlaGRkGAEFCUd  
  21. yaWRWaWV3MQ9nZMhHZ3iQZp62S8IR8fTJ5ZL42ira" /> 
  22. < /div> 
  23. ...  

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.

 
 
  1. protected override PageStatePersister PageStatePersister  
  2. {  
  3.     get 
  4.     {  
  5.         return new SessionPageStatePersister(this);  
  6.     }  
  7. }  

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.

 
 
  1. < input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"   
  2. value="/wEPaA8FDzhjNzkyNTMzNjE1YWEyNxgBBQlHcmlkVmlldzEPZ2QZw  
  3. 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:

 
 
  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Web;  
  5. using System.Web.Security;  
  6. using System.Web.UI;  
  7. using System.Web.UI.WebControls;  
  8. using System.Web.UI.WebControls.WebParts;  
  9. using System.Web.UI.HtmlControls;  
  10. public class PagePersisterBasePage : Page  
  11. {  
  12.     public PagePersisterBasePage()   {  
  13.     }  
  14.     protected override PageStatePersister PageStatePersister {  
  15.         get {  
  16.             return new SessionPageStatePersister(this);  
  17.         }  
  18.     }  
  19. }   

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.

 
 
  1. < %@ 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.

 
 
  1. < ?xml version="1.0"?> 
  2. < configuration> 
  3.    < system.web> 
  4.     < pages pageBaseType="PagePersisterBasePage" /> 
  5.    < /system.web> 
  6. < /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.

  1. Custom set of ASP. NET 2.0 data binding controls
  2. Example of a list control: HeadlineList
  3. ASP. NET 2.0 Data Binding Mechanism: Generate controls
  4. Brief Introduction to the Development of ASP. NET 2.0 Data Binding
  5. Overview ASP. NET Excel Process calling

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.