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.