ASP.net 2.0 page State continuation program

Source: Internet
Author: User
Tags sessions xmlns
Asp.net| Program | page

Developers of ASP.net controls use ViewState and control state to maintain state information between requests made by the browser. Typically, this information is passed to the client as a hidden field in the HTML markup that is rendered by the page. The page status is then passed back to the server and restored to the control or page as part of the next form submission. Even if the browser uses the HTTP protocol, which is defined as stateless, the developer of the control can easily provide a richer application experience with the ability to temporarily store state information.

ASP.net 2.0 allows you to modify the position and manner of temporarily maintaining page status. In some cases, it may be preferable to avoid sending data back and forth between the client and the server. ASP.net 2.0 provides two page state continuation programs that are hiddenfieldpagestate continuous (we've mentioned) and SessionPageStatePersister. SessionPageStatePersister uses server sessions related to browser sessions to store data. The use of SessionPageStatePersister has positive and negative two aspects. For pages sent to (from) browsers, the use of sessions (rather than hidden fields) avoids an increase in size. In many cases, the page state is an important part of all markup. However, storing data in a session consumes valuable server resources. In addition, the hidden field does not have a related timeout like a session. You can configure an application to hold the session to a back-end database and avoid adding the load directly to the WEB server. This will also extend to Web farm scenarios.

To use an ongoing program other than the default persistence program, you need to override the page's PageStatePersister property and return an instance of another persistent program. First, the following simple page populates a ArrayList with only a large number of digits, and then 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 to 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>
<title>untitled page</title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:gridview id= "GridView1" runat= "Server"/>
<asp:button id= "Button1" runat= "Server" text= "Submit"/></div>
</form>
</body>

When you view the HTML that the page renders, you see a large hidden field that is used to transfer ViewState.

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en"
"Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
Untitled Page
</title><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 overrides to the PageStatePersister property and use the built-in sessionpagestatepersister, the behavior of the page remains unchanged, but the storage for large amounts of state data is converted from hidden fields to session state.

protected override PageStatePersister PageStatePersister
{
Get
{
Return to New SessionPageStatePersister (this);
}
}

Note that in the source code of the page, the hidden field values are much smaller, but not completely disappeared. ASP.net will still transfer some of the smallest datasets in the page output.

<input type= "hidden" name= "__viewstate" id= "__viewstate"
Value= "/WEPAA8FDZHJNZKYNTMZNJE1YWEYNXGBBQLHCMLKVMLLDZEPZ2QZW
44jljfcglwrl9tinlie82yauq== "/>

In some scenarios, you might just want to add code similar to a smaller set of pages, so adding a simple rewrite like this might be acceptable. When you want a complete application or a larger set of pages to have this behavior, you need a more centralized way of controlling it. There are several ways to achieve this. We can move the code that created the continuation program into a class that inherits 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 to New SessionPageStatePersister (this);
}
}
}

ASP.net 2.0 enables you to specify the base type of a page using the "Inherits" page directive. ASP.net then 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 options allow us to set the page position so that all pages use a single base page type. In the Web.config page, we set the PageBaseType and do not need to add the Inherits property to any page to get the custom pagestatepersister behavior.

<?xml version= "1.0"?>
<configuration>
<system.web>
<pages pagebasetype= "Pagepersisterbasepage"/>
</system.web>
</configuration>

Changing PageStatePersister is not an easy thing to do. Please carefully consider your application and deployment. Although there is a cost associated with round-trip ViewState in a hidden field, there is a need for direct server resource consumption to keep the state there. As you can see from the previous example, you can insert a custom persistence program to store the state in another place, such as a back-end database or a state service shared by a Web farm. In addition, as we demonstrated, you can centrally control the behavior of your application, or control it on the page by page.



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.