Differences between ViewState and Session)

Source: Internet
Author: User

Recently, when I made a paging control, I found that the current index pages were recorded with ViewState instead of Session. Again, there was almost no difference in the use of the two. Are they exactly the same. I have carefully checked and tried to find the differences between the two, which are very subtle, but we should pay attention to them in developing WEB pages.
If the session value is stored in the server memory, it is certain that a large number of sessions will increase the server load. viewstate stores data in the page hidden control and no longer occupies server resources. Therefore, we can save some variables and objects that need to be remembered by the server to viewstate. sesson should only be applied to variables and object storage that need to span pages and are related to each access user. in addition, the session will expire in 20 minutes by default, while the viewstate will never expire.
However, viewstate does not store all. net data. It only supports String, Integer, Boolean, Array, ArrayList, Hashtable, and custom data types.
Of course, everything has two sides. Using viewstate will increase the page html output and occupy more bandwidth, which requires careful consideration. in addition, because all viewstates are stored in a hidden field, you can easily view the base64 encoded value by viewing the source code. after conversion, you can get the objects and variable values in your storage.

The Load Code for the paging Repeater control is as follows:

Protected void Page_Load (object sender, EventArgs e)
{
BraveboyDataTable = (DataTable) BraveboyRepeater. DataSource; // obtain the data source
If (! Page. IsPostBack)
{
CurrentPage = 0;
ViewState ["PageIndex"] = 0;

// Calculate the total number of records
RecordCount = CalculateRecord ();
// Calculate the total number of pages
PageCount = (RecordCount + PageSize-1)/PageSize;
LblPageCount. Text = PageCount. ToString ();
ViewState ["PageCount"] = PageCount;
BraveboyBindData ();
}
CurrentPage = (int) ViewState ["PageIndex"];
PageCount = (int) ViewState ["PageCount"];
}

If there are no last two sentences
CurrentPage = (int) ViewState ["PageIndex"];
PageCount = (int) ViewState ["PageCount"];
Every time you click to flip the page, CurrentPage and PageCount are unknown, and ViewCount is a good solution to this problem.
Note: The value of ViewState ["PageIndex"] is modified every time you flip the page.

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.